22
33import java .io .File ;
44import java .io .FileInputStream ;
5+ import java .io .FileNotFoundException ;
56import java .io .IOException ;
67import java .io .OutputStream ;
78import java .io .Reader ;
@@ -600,36 +601,37 @@ public Map<String, String> getLocalization(ArrayList<IAttributeDefinition> attri
600601
601602 @ SuppressWarnings ("deprecation" )
602603 @ Override
603- public Oid saveAttachment (String filePath , Asset asset , String attachmentName ) throws V1Exception , IOException {
604-
604+ public Oid saveAttachment (String filePath , Asset asset , String attachmentName ) throws V1Exception , IOException {
605605 if (StringUtils .isEmpty (filePath ))
606606 throw new NullArgumentException ("filePath" );
607- File f = new File (Thread .currentThread ().getContextClassLoader ().getResource (filePath ).toString ());
607+
608+ File file = new File (filePath );
609+ if (!file .exists ())
610+ throw new FileNotFoundException (String .format ("File \" %s\" does not exist." , filePath ));
608611
609- String mimeType = MimeType .resolve (filePath );
610-
611- IAssetType attachmentType = _meta .getAssetType ("Attachment" );
612- IAttributeDefinition attachmentAssetDef = attachmentType .getAttributeDefinition ("Asset" );
613- IAttributeDefinition attachmentContent = attachmentType .getAttributeDefinition ("Content" );
614- IAttributeDefinition attachmentContentType = attachmentType .getAttributeDefinition ("ContentType" );
615- IAttributeDefinition attachmentFileName = attachmentType .getAttributeDefinition ("Filename" );
616- IAttributeDefinition attachmentNameAttr = attachmentType .getAttributeDefinition ("Name" );
617- Asset attachment = createNew (attachmentType , Oid .Null );
618- attachment .setAttributeValue (attachmentNameAttr , attachmentName );
619- attachment .setAttributeValue (attachmentFileName , filePath );
620- attachment .setAttributeValue (attachmentContentType , mimeType );
621- attachment .setAttributeValue (attachmentContent , "" );
622- attachment .setAttributeValue (attachmentAssetDef , asset .getOid ());
623- save (attachment );
612+ String mimeType = MimeType .resolve (filePath );
613+ IAssetType attachmentType = _meta .getAssetType ("Attachment" );
614+ IAttributeDefinition attachmentAssetDef = attachmentType .getAttributeDefinition ("Asset" );
615+ IAttributeDefinition attachmentContent = attachmentType .getAttributeDefinition ("Content" );
616+ IAttributeDefinition attachmentContentType = attachmentType .getAttributeDefinition ("ContentType" );
617+ IAttributeDefinition attachmentFileName = attachmentType .getAttributeDefinition ("Filename" );
618+ IAttributeDefinition attachmentNameAttr = attachmentType .getAttributeDefinition ("Name" );
619+ Asset attachment = createNew (attachmentType , Oid .Null );
620+ attachment .setAttributeValue (attachmentNameAttr , attachmentName );
621+ attachment .setAttributeValue (attachmentFileName , filePath );
622+ attachment .setAttributeValue (attachmentContentType , mimeType );
623+ attachment .setAttributeValue (attachmentContent , "" );
624+ attachment .setAttributeValue (attachmentAssetDef , asset .getOid ());
625+ save (attachment );
624626
625627 String key = attachment .getOid ().getKey ().toString ();
626- FileInputStream inStream = new FileInputStream (Thread . currentThread (). getContextClassLoader (). getResource ( filePath ). getPath () );
628+ FileInputStream inStream = new FileInputStream (filePath );
627629 OutputStream output = _connector != null ? _connector .beginRequest (key .substring (key .lastIndexOf ('/' ) + 1 ), null ) : _v1Connector .beginRequest (key .substring (key .lastIndexOf ('/' ) + 1 ), null );
628630 byte [] buffer = new byte [inStream .available () + 1 ];
629- while (true ){
631+ while (true ) {
630632 int read = inStream .read (buffer , 0 , buffer .length );
631633 if (read <= 0 )
632- break ;
634+ break ;
633635 output .write (buffer , 0 , read );
634636 }
635637
@@ -647,70 +649,76 @@ public Oid saveAttachment(String filePath, Asset asset, String attachmentName) t
647649
648650
649651 @ Override
650- public Reader getAttachment (Oid attachmentOid ) throws V1Exception {
651- Reader result = null ;
652- if (_connector != null ){
653- result = _connector .getData (attachmentOid .getKey ().toString ());
654- } else if (_v1Connector != null ) {
655- _v1Connector .useAttachmentApi ();
656- result = _v1Connector .getData (attachmentOid .getKey ().toString ());
657- }
652+ public Reader getAttachment (Oid attachmentOid ) throws V1Exception {
653+ Reader result = null ;
654+ if (_connector != null ) {
655+ result = _connector .getData (attachmentOid .getKey ().toString ());
656+ } else if (_v1Connector != null ) {
657+ _v1Connector .useAttachmentApi ();
658+ result = _v1Connector .getData (attachmentOid .getKey ().toString ());
659+ }
658660
659- return result ;
660- }
661+ return result ;
662+ }
661663
662664 @ Override
663- public Oid saveEmbeddedImage (String filePath , Asset asset ) throws V1Exception , IOException {
664- if (StringUtils .isEmpty (filePath ))
665- throw new NullArgumentException ("Null value " + filePath );
666- File f = new File (Thread .currentThread ().getContextClassLoader ().getResource (filePath ).toString ());
667-
668- String mimeType = MimeType .resolve (filePath );
669- IAssetType embeddedImageType = _meta .getAssetType ("EmbeddedImage" );
670- Asset newEmbeddedImage = createNew (embeddedImageType , Oid .Null );
671- IAttributeDefinition assetAttribute = embeddedImageType .getAttributeDefinition ("Asset" );
672- IAttributeDefinition contentAttribute = embeddedImageType .getAttributeDefinition ("Content" );
673- IAttributeDefinition contentTypeAttribute = embeddedImageType .getAttributeDefinition ("ContentType" );
674- newEmbeddedImage .setAttributeValue (assetAttribute , asset .getOid ());
675- newEmbeddedImage .setAttributeValue (contentTypeAttribute , mimeType );
676- newEmbeddedImage .setAttributeValue (contentAttribute , "" );
677- save (newEmbeddedImage );
678-
679- String key = newEmbeddedImage .getOid ().getKey ().toString ();
680- FileInputStream inStream = new FileInputStream (Thread .currentThread ().getContextClassLoader ().getResource (filePath ).getPath ());
681- OutputStream output = _connector != null ? _connector .beginRequest (key .substring (key .lastIndexOf ('/' ) + 1 ), null ) : _v1Connector .beginRequest (key .substring (key .lastIndexOf ('/' ) + 1 ),null );
682- byte [] buffer = new byte [inStream .available () + 1 ];
683- while (true ){
684- int read = inStream .read (buffer , 0 , buffer .length );
685- if (read <= 0 )
686- break ;
687-
688- output .write (buffer , 0 , read );
689- }
690-
691- if (_connector != null ){
692- _connector .endRequest (key .substring (key .lastIndexOf ('/' ) + 1 ));
693- }
694- else {
695- _v1Connector .useEmbeddedApi ();
696- _v1Connector .endRequest (key .substring (key .lastIndexOf ('/' ) + 1 ));
697- }
665+ public Oid saveEmbeddedImage (String filePath , Asset asset ) throws V1Exception , IOException {
666+ if (StringUtils .isEmpty (filePath ))
667+ throw new NullArgumentException ("Null value " + filePath );
668+
669+ File file = new File (filePath );
670+ if (!file .exists ())
671+ throw new FileNotFoundException (String .format ("File \" %s\" does not exist." , filePath ));
672+
673+ String mimeType = MimeType .resolve (filePath );
674+ IAssetType embeddedImageType = _meta .getAssetType ("EmbeddedImage" );
675+ Asset newEmbeddedImage = createNew (embeddedImageType , Oid .Null );
676+ IAttributeDefinition assetAttribute = embeddedImageType
677+ .getAttributeDefinition ("Asset" );
678+ IAttributeDefinition contentAttribute = embeddedImageType
679+ .getAttributeDefinition ("Content" );
680+ IAttributeDefinition contentTypeAttribute = embeddedImageType
681+ .getAttributeDefinition ("ContentType" );
682+ newEmbeddedImage .setAttributeValue (assetAttribute , asset .getOid ());
683+ newEmbeddedImage .setAttributeValue (contentTypeAttribute , mimeType );
684+ newEmbeddedImage .setAttributeValue (contentAttribute , "" );
685+ save (newEmbeddedImage );
686+
687+ String key = newEmbeddedImage .getOid ().getKey ().toString ();
688+ FileInputStream inStream = new FileInputStream (filePath );
689+ OutputStream output = _connector != null ? _connector .beginRequest (
690+ key .substring (key .lastIndexOf ('/' ) + 1 ), null ) : _v1Connector
691+ .beginRequest (key .substring (key .lastIndexOf ('/' ) + 1 ), null );
692+ byte [] buffer = new byte [inStream .available () + 1 ];
693+ while (true ) {
694+ int read = inStream .read (buffer , 0 , buffer .length );
695+ if (read <= 0 )
696+ break ;
697+
698+ output .write (buffer , 0 , read );
699+ }
700+
701+ if (_connector != null ) {
702+ _connector .endRequest (key .substring (key .lastIndexOf ('/' ) + 1 ));
703+ } else {
704+ _v1Connector .useEmbeddedApi ();
705+ _v1Connector .endRequest (key .substring (key .lastIndexOf ('/' ) + 1 ));
706+ }
707+
708+ return newEmbeddedImage .getOid ();
709+ }
698710
699- return newEmbeddedImage .getOid ();
700- }
701-
702711 @ Override
703- public Reader getEmbeddedImage (Oid embeddedImageOid ) throws V1Exception {
704- Reader result = null ;
705- if (_connector != null ){
706- result = _connector .getData (embeddedImageOid .getKey ().toString ());
707- }
708- else if (_v1Connector != null ){
709- _v1Connector .useEmbeddedApi ();
710- result = _v1Connector .getData (embeddedImageOid .getKey ().toString ());
711- }
712+ public Reader getEmbeddedImage (Oid embeddedImageOid ) throws V1Exception {
713+ Reader result = null ;
714+ if (_connector != null ) {
715+ result = _connector .getData (embeddedImageOid .getKey ().toString ());
716+ } else if (_v1Connector != null ) {
717+ _v1Connector .useEmbeddedApi ();
718+ result = _v1Connector .getData (embeddedImageOid .getKey ().toString ());
719+ }
712720
713- return result ;
714- }
721+ return result ;
722+ }
715723
716724}
0 commit comments