a3e73b6a284a80abf516d78d0427b9e23771100a
[mediacube.git] /
1 package user.jobengine.server.steps;\r
2 \r
3 import java.io.OutputStream;\r
4 import java.net.InetAddress;\r
5 import java.net.URI;\r
6 import java.util.ArrayList;\r
7 import java.util.Calendar;\r
8 import java.util.Date;\r
9 import java.util.List;\r
10 \r
11 import org.apache.commons.lang.StringUtils;\r
12 import org.apache.commons.net.ftp.FTP;\r
13 import org.apache.commons.net.ftp.FTPClient;\r
14 import org.apache.commons.net.ftp.FTPReply;\r
15 import org.apache.logging.log4j.LogManager;\r
16 import org.apache.logging.log4j.Logger;\r
17 import org.apache.logging.log4j.Marker;\r
18 \r
19 import com.ibm.nosql.json.api.BasicDBObject;\r
20 import com.ibm.nosql.json.api.DB;\r
21 import com.ibm.nosql.json.api.DBCollection;\r
22 import com.ibm.nosql.json.api.DBCursor;\r
23 import com.ibm.nosql.json.api.DBObject;\r
24 \r
25 import user.commons.CalendarUtils;\r
26 import user.commons.ListUtils;\r
27 import user.commons.StoreUri;\r
28 import user.commons.nosql.NoSQLUtils;\r
29 import user.commons.octopus.IOctopusAPI;\r
30 import user.commons.octopus.OctopusAPI;\r
31 import user.commons.remotestore.FtpDirectoryLister;\r
32 import user.commons.remotestore.RemoteStoreProtocol;\r
33 import user.jobengine.db.IItemManager;\r
34 import user.jobengine.server.IJobEngine;\r
35 import user.jobengine.server.IJobRuntime;\r
36 import user.jobengine.server.steps.MetadataTypeDetector.MetadataType;\r
37 \r
38 public class CopyForArchiveNEXIOMaterialsStep extends JobStep {\r
39         private static final String SCHEDULED_FORMAT = "yyyy.MM.dd HH:mm";\r
40         private static final Logger logger = LogManager.getLogger();\r
41         private static final String UTF_8 = "utf-8";\r
42         private static final String JSON_EXT = ".json";\r
43         private static final String XML_EXT = ".xml";\r
44         private static final String DURATION = "duration";\r
45         private static final String MXFEXT = ".MXF";\r
46         private static final String NEXIOCLIPS = "nexioclips";\r
47         private static final String LONGNAMEID = "longnameid";\r
48         private static final String ARCHIVEDRUNDOWNS = "archivedrundowns";\r
49         private static final String ID = "id";\r
50         private static final String MEDIATYPE = "Hír bejátszó";\r
51         private OctopusAPI octopusAPI;\r
52         private IItemManager manager;\r
53 \r
54         private DB db;\r
55         private FTPClient sourceFtp;\r
56         private FTPClient targetFtp;\r
57         private StoreUri sourceUri;\r
58         private StoreUri targetUri;\r
59         private int nexioKillDateDays;\r
60         private String nexioAgency;\r
61         private Marker systemMarker;\r
62         private List<String> transferredFileNames = null;\r
63 \r
64         private int check(int value, String name) {\r
65                 if (value == 0) {\r
66                         logger.error(systemMarker, "A folyamat '{}' bemeneti paramétere 0.", name);\r
67                         throw new NullPointerException(String.format("System is not configured properly, missing '%s' input parameter.", name));\r
68                 }\r
69                 return value;\r
70         }\r
71 \r
72         private String check(String value, String name) {\r
73                 if (value == null) {\r
74                         logger.error(systemMarker, "A folyamat '{}' bemeneti paramétere üres.", name);\r
75                         throw new NullPointerException(String.format("System is not configured properly, missing '%s' input parameter.", name));\r
76                 }\r
77                 return value;\r
78         }\r
79 \r
80         private void copy(RundownArchive rundownArchive) throws Exception {\r
81                 for (StoryArchive storyArchive : rundownArchive.getStoryArchives()) {\r
82                         for (FileArchive fileArchive : storyArchive.getFileArchives()) {\r
83                                 try {\r
84                                         copyFile(fileArchive, rundownArchive, storyArchive);\r
85                                 } catch (Exception e) {\r
86                                         logger.error(systemMarker, "A '{}' clip archiválása sikertelen. A rendszer üzenete: {}", fileArchive.getFileName(), e.getMessage());\r
87                                 }\r
88                         }\r
89                 }\r
90         }\r
91 \r
92         private void copyFile(FileArchive fileArchive, RundownArchive rundownArchive, StoryArchive storyArchive) throws Exception {\r
93                 String origFileName = fileArchive.getFileName();\r
94                 String fileName = String.format("%s-%s", origFileName, rundownArchive.getItemHouseId());\r
95                 String videoFileName = fileName + MXFEXT;\r
96 \r
97                 if (transferredFileNames == null)\r
98                         transferredFileNames = new ArrayList<>();\r
99 \r
100                 //A mar letezo mozikat nem archivaljuk le ujra, csak a metaadatot\r
101                 long existingMediaId = 0;\r
102                 if (transferredFileNames.contains(origFileName)) {\r
103                         logger.info(systemMarker, "A '{}' file archiválásra felkészítése egy másik story kapcsán már megtörtént, ezért csak metaadat archiválás szükséges.",\r
104                                         origFileName);\r
105                         transferChunk(videoFileName);\r
106                 } else {\r
107                         existingMediaId = ItemManagerExtensions.getExistingRundownMedia(manager, origFileName);\r
108                         if (existingMediaId == 0) {\r
109                                 transferredFileNames.add(origFileName);\r
110                                 transferFile(origFileName + MXFEXT, videoFileName);\r
111                         } else {\r
112                                 logger.info(systemMarker, "A '{}' file archiválása már megtörtént, ezért csak metaadat archiválás szükséges.", origFileName);\r
113                                 transferChunk(videoFileName);\r
114                         }\r
115                 }\r
116 \r
117                 BasicDBObject metadata = createMetadata(rundownArchive, storyArchive, fileArchive, existingMediaId);\r
118                 transferMetadata(videoFileName, metadata);\r
119                 createSourceKillDateFile(rundownArchive, origFileName);\r
120         }\r
121 \r
122         private BasicDBObject createMetadata(RundownArchive rundownArchive, StoryArchive storyArchive, FileArchive fileArchive, long existingMediaId) {\r
123                 BasicDBObject result = new BasicDBObject();\r
124                 result.put("itemHouseId", rundownArchive.getItemHouseId());\r
125                 result.put("itemTitle", rundownArchive.getItemTitle());\r
126                 result.put("itemDescription", rundownArchive.getItemDesc());\r
127                 result.put("userName", "mediacube");\r
128                 result.put("mediaHouseId", storyArchive.getMediaHouseId());\r
129                 result.put("mediaTitle", storyArchive.getMediaTitle());\r
130                 result.put("mediaDescription", storyArchive.getMediaDesc());\r
131                 result.put("mediaType", MEDIATYPE);\r
132                 result.put("duration", fileArchive.getDuration());\r
133                 result.put("existingMediaId", existingMediaId);\r
134                 return result;\r
135         }\r
136 \r
137         private void createSourceKillDateFile(RundownArchive rundownArchive, String fileName) throws Exception {\r
138                 logger.info("Create killdate/agency for {}", fileName);\r
139                 OutputStream outStream = null;\r
140                 try {\r
141                         sourceFtp = ((FtpDirectoryLister) sourceUri.getLister()).connect();\r
142                         Calendar killDate = CalendarUtils.createCalendar(rundownArchive.getScheduleDate());\r
143                         killDate.add(Calendar.DAY_OF_YEAR, nexioKillDateDays);\r
144                         byte[] killDateFile = EscortFiles.createNEXIOKillDateFile(fileName, killDate.getTime(), null, nexioAgency);\r
145                         outStream = sourceFtp.storeFileStream(fileName + XML_EXT);\r
146                         if (outStream == null) {\r
147                                 throw new NullPointerException("Can not open: " + fileName + XML_EXT + " Reply:" + sourceFtp.getReplyString());\r
148                         }\r
149                         outStream.write(killDateFile);\r
150                         outStream.flush();\r
151                 } catch (Exception e) {\r
152                         logger.catching(e);\r
153                         throw e;\r
154                 } finally {\r
155                         if (outStream != null)\r
156                                 outStream.close();\r
157                         sourceUri.cleanUp();\r
158                 }\r
159         }\r
160 \r
161         @StepEntry\r
162         public Object[] execute(int nexioPort, String nexioUserName, String nexioPassword, String archiveFtp, String archiveUserName, String archivePassword,\r
163                         int daysBeforeNow, int nexioKillDateDays, String nexioAgency, IJobEngine jobEngine, IJobRuntime jobRuntime) throws Exception {\r
164                 systemMarker = jobRuntime.getMarker();\r
165                 setAndCheck(nexioPort, nexioUserName, nexioPassword, archiveFtp, archiveUserName, archivePassword, nexioKillDateDays, nexioAgency, jobEngine);\r
166                 octopusAPI = new OctopusAPI();\r
167                 Calendar scheduledDate = Calendar.getInstance();\r
168                 scheduledDate.add(Calendar.DAY_OF_YEAR, -1 * daysBeforeNow);\r
169                 List<DBObject> rundowns = octopusAPI.getRundowns(scheduledDate.getTime());\r
170                 if (rundowns == null) {\r
171                         logger.warn(systemMarker, "Nem található adástükör a {} napra.", CalendarUtils.toDateString(scheduledDate));\r
172                         return null;\r
173                 }\r
174 \r
175                 processRundowns(rundowns);\r
176                 return null;\r
177         }\r
178 \r
179         //      private String getVersionedFileName(String fileName, String extension) throws Exception {\r
180         //              String result = fileName;\r
181         //              try {\r
182         //                      targetFtp = ((FtpDirectoryLister) targetUri.getLister()).connect();\r
183         //                      FTPFile[] listFiles = targetFtp.listFiles();\r
184         //                      List<String> fileNames = new ArrayList<>();\r
185         //                      for (FTPFile ftpFile : listFiles) {\r
186         //                              fileNames.add(ftpFile.getName());\r
187         //                      }\r
188         //                      while (fileNames.contains(result + extension)) {\r
189         //\r
190         //                      }\r
191         //\r
192         //              } catch (Exception e) {\r
193         //                      logger.catching(e);\r
194         //                      throw e;\r
195         //              } finally {\r
196         //                      targetUri.cleanUp();\r
197         //              }\r
198         //              return result;\r
199         //      }\r
200 \r
201         private FileArchive processMosObject(BasicDBObject rundown, BasicDBObject story, BasicDBObject mosObject) throws Exception {\r
202                 String mosID = mosObject.getString(IOctopusAPI.OBJ_ID);\r
203                 if (MetadataTypeDetector.GuessMetadataType(mosID) != MetadataType.OctopusPlaceholder) {\r
204                         logger.trace("Skipping MOS object {}", mosID);\r
205                         return null;\r
206                 }\r
207                 DBCollection clips = db.getCollection(NEXIOCLIPS);\r
208                 BasicDBObject clip = (BasicDBObject) clips.findOne(new BasicDBObject(LONGNAMEID, mosID));\r
209                 if (clip == null) {\r
210                         logger.debug("File {} NOT exists", mosID);\r
211                         return null;\r
212                         //throw new Exception(String.format("File NOT exists %s", mosID));\r
213                 } else {\r
214                         logger.debug("File {} exists", mosID);\r
215                 }\r
216                 long duration = NoSQLUtils.asLong(clip, DURATION);\r
217 \r
218                 if (duration == 0) {\r
219                         logger.debug("File {} exists with 0 frame length", mosID);\r
220                         return null;\r
221                 }\r
222                 if (duration == 1) {\r
223                         logger.debug("File {} exists with 1 frame length", mosID);\r
224                         return null;\r
225                 }\r
226                 return new FileArchive(mosID, duration);\r
227         }\r
228 \r
229         private RundownArchive processRundow(DBObject r) throws Exception {\r
230                 BasicDBObject rundown = (BasicDBObject) r;\r
231                 long rundownID = rundown.getLong(ID);\r
232                 logger.info("Processing rundown {} {}", rundownID, rundown.getString(IOctopusAPI.NAME));\r
233                 List<DBObject> stories = octopusAPI.getRundownFullStories(rundownID);\r
234                 if (stories == null)\r
235                         return null;\r
236                 RundownArchive result = new RundownArchive();\r
237 \r
238                 long id = NoSQLUtils.asLong(rundown, IOctopusAPI.ID);\r
239                 if (id == 0)\r
240                         return null;\r
241                 String name = NoSQLUtils.asString(NoSQLUtils.asDBObject(rundown, IOctopusAPI.RUNDOWN_TYPE), IOctopusAPI.NAME);\r
242                 if (StringUtils.isBlank(name))\r
243                         return null;\r
244                 String channel = NoSQLUtils.asString(NoSQLUtils.asDBObject(rundown, IOctopusAPI.CHANNEL), IOctopusAPI.NAME);\r
245                 Date scheduledStart = rundown.getDate(IOctopusAPI.SCHEDULED_START);\r
246                 if (scheduledStart == null)\r
247                         return null;\r
248                 String start = CalendarUtils.toString(CalendarUtils.createCalendar(scheduledStart), SCHEDULED_FORMAT);\r
249                 result.setScheduleDate(scheduledStart);\r
250                 result.setItemHouseId(String.valueOf(id));\r
251                 result.setItemTitle(String.format("%s %s %s", start, name, channel));\r
252 \r
253                 for (DBObject s : stories) {\r
254                         StoryArchive storyArchive = processStory(rundown, s);\r
255                         if (storyArchive == null)\r
256                                 continue;\r
257                         result.addStoryArchive(storyArchive);\r
258                 }\r
259                 return result;\r
260         }\r
261 \r
262         private void processRundowns(List<DBObject> rundowns) {\r
263 \r
264                 //TODO kiveni publikálás előtt\r
265                 //db.getCollection(ARCHIVEDRUNDOWNS).drop();\r
266                 List<BasicDBObject> archivedRundowns = queryArchivedRundowns();\r
267 \r
268                 int index = 1;\r
269 \r
270                 for (DBObject r : rundowns) {\r
271                         BasicDBObject rundown = (BasicDBObject) r;\r
272                         setProgress(index * 100 / rundowns.size());\r
273                         long rundownID = NoSQLUtils.asLong(rundown, IOctopusAPI.ID);\r
274                         String rundownName = NoSQLUtils.asString(rundown, IOctopusAPI.NAME);\r
275                         try {\r
276                                 BasicDBObject currentRundownID = new BasicDBObject(IOctopusAPI.ID, rundownID);\r
277                                 if (archivedRundowns != null && archivedRundowns.contains(currentRundownID)) {\r
278                                         logger.info("Skipping archived rundown {} {}", rundownID, rundownName);\r
279                                         continue;\r
280                                 }\r
281 \r
282                                 RundownArchive rundownArchive = processRundow(r);\r
283                                 if (rundownArchive == null || rundownArchive.isEmpty()) {\r
284                                         logger.info("Skipping rundown {} {}", rundownID, rundownName);\r
285                                         continue;\r
286                                 }\r
287 \r
288                                 logger.info("Saving rundown {} {}", rundownID, rundownName);\r
289                                 copy(rundownArchive);\r
290                                 logger.info(systemMarker, "A '{}' tükör {}db bejátszójának archiválása sikeres volt", rundownArchive.getItemTitle(),\r
291                                                 rundownArchive.getStoryArchives().size());\r
292 \r
293                                 db.getCollection(ARCHIVEDRUNDOWNS).save(currentRundownID);\r
294                         } catch (Exception e) {\r
295                                 logger.catching(e);\r
296                                 logger.error(systemMarker,\r
297                                                 String.format("A %s %s tükör archiválása nem lehetséges, mert a annak ellenőrzése hibát jelzett. A rendszer üzenete: %s", rundownID,\r
298                                                                 rundownName, e.getMessage()));\r
299                         }\r
300                         index++;\r
301                 }\r
302 \r
303         }\r
304 \r
305         private StoryArchive processStory(BasicDBObject rundown, DBObject s) throws Exception {\r
306                 BasicDBObject story = (BasicDBObject) s;\r
307                 String parentStoryID = story.getString(IOctopusAPI.PARENT_STORY_ID);\r
308                 if (StringUtils.isBlank(parentStoryID)) {\r
309                         logger.warn("Story parentStoryID is null: {}", story.toPrettyString(null));\r
310                         return null;\r
311                 } else\r
312                         logger.debug("Processing story {}", parentStoryID);\r
313                 List<BasicDBObject> mosObjects = NoSQLUtils.asList(story, IOctopusAPI.MOS_OBJECTS);\r
314                 if (mosObjects == null)\r
315                         return null;\r
316                 StoryArchive storyArchive = null;\r
317                 for (BasicDBObject mosObject : mosObjects) {\r
318                         FileArchive fileArchive = processMosObject(rundown, story, mosObject);\r
319                         if (fileArchive == null)\r
320                                 continue;\r
321                         if (storyArchive == null) {\r
322                                 storyArchive = new StoryArchive();\r
323                                 storyArchive.setMediaHouseId(parentStoryID);\r
324                                 storyArchive.setMediaTitle(story.getString(IOctopusAPI.NAME));\r
325                                 storyArchive.setMediaDesc(story.getString(IOctopusAPI.SCRIPT_CONTENT));\r
326 \r
327                         }\r
328                         storyArchive.addFileArchive(fileArchive);\r
329                 }\r
330                 return storyArchive;\r
331 \r
332         }\r
333 \r
334         private List<BasicDBObject> queryArchivedRundowns() {\r
335                 List<BasicDBObject> result = null;\r
336                 DBCollection collection = db.getCollection(ARCHIVEDRUNDOWNS);\r
337                 DBCursor find = collection.find(new BasicDBObject(), new BasicDBObject(IOctopusAPI._ID, 0).append(IOctopusAPI.ID, 1));\r
338                 if (find.hasNext())\r
339                         result = ListUtils.cast(find.toArray());\r
340                 return result;\r
341         }\r
342 \r
343         private void setAndCheck(int nexioPort, String nexioUserName, String nexioPassword, String archiveFtp, String archiveUserName, String archivePassword,\r
344                         int nexioKillDateDays, String nexioAgency, IJobEngine jobEngine) throws Exception {\r
345                 db = NoSQLUtils.getNoSQLDB();\r
346                 if (db == null) {\r
347                         logger.error(systemMarker, "Az NoSQL adatkezelő réteg nem elérhető.");\r
348                         throw new NullPointerException("Internal error, missing NoSQL DB reference.");\r
349                 }\r
350 \r
351                 if (jobEngine == null) {\r
352                         logger.error(systemMarker, "Az folyamatkezelő réteg nem elérhető.");\r
353                         throw new NullPointerException("Internal error, missing JobEngine reference.");\r
354                 }\r
355                 manager = jobEngine.getItemManager();\r
356                 if (manager == null) {\r
357                         logger.error(systemMarker, "Az adatbáziskezelő réteg nem elérhető.");\r
358                         throw new NullPointerException("Internal error, missing ItemManager reference.");\r
359                 }\r
360                 String nexioHost = System.getProperty("nexio.host");\r
361                 if (StringUtils.isBlank(nexioHost)) {\r
362                         logger.error(systemMarker, "A 'nexio.host' rendszer paraméter nem található.");\r
363                         throw new NullPointerException("System is not configured properly, 'jobengine.selenio.address' startup parameter missing.");\r
364                 }\r
365                 check(nexioPort, "nexioPort");\r
366                 check(nexioUserName, "nexioUserName");\r
367                 check(nexioPassword, "nexioPassword");\r
368 \r
369                 check(nexioKillDateDays, "nexioKillDateDays");\r
370                 this.nexioKillDateDays = nexioKillDateDays;\r
371                 check(nexioAgency, "nexioAgency");\r
372                 this.nexioAgency = nexioAgency;\r
373 \r
374                 sourceUri = manager.createStoreUri(RemoteStoreProtocol.FTP, nexioHost);\r
375                 sourceUri.setPortNumber(nexioPort);\r
376                 sourceUri.setUserName(nexioUserName);\r
377                 sourceUri.setPassword(nexioPassword);\r
378                 if (sourceUri == null) {\r
379                         logger.error(systemMarker, "A forrás nem elérhető.");\r
380                         throw new NullPointerException("Internal error, missing 'sourceUri'.");\r
381                 }\r
382 \r
383                 check(archiveFtp, "archiveFtp");\r
384                 check(archiveUserName, "archiveUserName");\r
385                 check(archivePassword, "archivePassword");\r
386 \r
387                 targetUri = manager.createStoreUri(new URI(archiveFtp));\r
388                 targetUri.setUserName(archiveUserName);\r
389                 targetUri.setPassword(archivePassword);\r
390                 if (targetUri == null) {\r
391                         logger.error(systemMarker, "A cél nem elérhető.");\r
392                         throw new NullPointerException("Internal error, missing 'targetUri'.");\r
393                 }\r
394 \r
395         }\r
396 \r
397         private void transferChunk(String fileName) throws Exception {\r
398                 logger.info("Transfer chunk {}", fileName);\r
399                 OutputStream outStream = null;\r
400                 try {\r
401                         targetFtp = ((FtpDirectoryLister) targetUri.getLister()).connect();\r
402                         outStream = targetFtp.storeFileStream(fileName);\r
403                         if (outStream == null) {\r
404                                 throw new NullPointerException("Can not open: " + fileName + MXFEXT + " Reply:" + targetFtp.getReplyString());\r
405                         }\r
406                 } catch (Exception e) {\r
407                         logger.catching(e);\r
408                         throw e;\r
409                 } finally {\r
410                         if (outStream != null)\r
411                                 outStream.close();\r
412                         targetUri.cleanUp();\r
413                 }\r
414         }\r
415 \r
416         private void transferFile(String sourceFileName, String targetFileName) throws Exception {\r
417                 int reply = 0;\r
418                 logger.info("Transfer clip {}", sourceFileName);\r
419                 try {\r
420                         sourceFtp = ((FtpDirectoryLister) sourceUri.getLister()).connect();\r
421                         targetFtp = ((FtpDirectoryLister) targetUri.getLister()).connect();\r
422                         if (!targetFtp.enterRemotePassiveMode())\r
423                                 throw new Exception("!PASV");\r
424 \r
425                         reply = sourceFtp.port(InetAddress.getByName(targetFtp.getPassiveHost()), targetFtp.getPassivePort());\r
426                         if (!FTPReply.isPositiveCompletion(reply))\r
427                                 throw new Exception("PORT parancs válasza: " + sourceFtp.getReplyString());\r
428 \r
429                         if (!sourceFtp.setFileType(FTP.BINARY_FILE_TYPE))\r
430                                 throw new Exception("!SOURCE TYPE");\r
431 \r
432                         reply = sourceFtp.retr(sourceFileName);\r
433                         if (!FTPReply.isPositivePreliminary(reply))\r
434                                 throw new Exception("RETR parancs válasza: " + sourceFtp.getReplyString());\r
435 \r
436                         if (!targetFtp.setFileType(FTP.BINARY_FILE_TYPE))\r
437                                 throw new Exception("!TARGET TYPE");\r
438 \r
439                         reply = targetFtp.stor(targetFileName);\r
440                         if (!FTPReply.isPositivePreliminary(reply))\r
441                                 throw new Exception("STOR parancs válasza: " + sourceFtp.getReplyString());\r
442 \r
443                         while (true) {\r
444                                 reply = sourceFtp.stat();\r
445                                 if (!FTPReply.isPositiveCompletion(reply))\r
446                                         throw new Exception("STAT parancs válasza: " + sourceFtp.getReplyString());\r
447 \r
448                                 logger.info("Status: {}", sourceFtp.getReplyString());\r
449                                 if (reply == 226) {\r
450                                         break;\r
451                                 }\r
452                                 Thread.sleep(1000);\r
453                         }\r
454                 } catch (Exception e) {\r
455                         logger.catching(e);\r
456                         throw e;\r
457                 } finally {\r
458                         sourceUri.cleanUp();\r
459                         targetUri.cleanUp();\r
460                 }\r
461 \r
462         }\r
463 \r
464         private void transferMetadata(String fileName, BasicDBObject metadata) throws Exception {\r
465                 logger.info("Transfer metadata {}", fileName);\r
466                 OutputStream outStream = null;\r
467                 try {\r
468                         targetFtp = ((FtpDirectoryLister) targetUri.getLister()).connect();\r
469                         if (!targetFtp.changeWorkingDirectory(EscortFiles.STATUSFOLDER)) {\r
470                                 targetFtp.makeDirectory(EscortFiles.STATUSFOLDER);\r
471                                 if (!targetFtp.changeWorkingDirectory(EscortFiles.STATUSFOLDER))\r
472                                         throw new Exception("!STATUSFOLDER");\r
473                         }\r
474 \r
475                         outStream = targetFtp.storeFileStream(fileName + JSON_EXT);\r
476                         if (outStream == null) {\r
477                                 throw new NullPointerException("Can not open: " + fileName + JSON_EXT + " Reply:" + targetFtp.getReplyString());\r
478                         }\r
479                         outStream.write(metadata.toString().getBytes(UTF_8));\r
480                         outStream.flush();\r
481                         //targetFtp.changeToParentDirectory();\r
482                 } catch (Exception e) {\r
483                         logger.catching(e);\r
484                         throw e;\r
485                 } finally {\r
486                         if (outStream != null)\r
487                                 outStream.close();\r
488                         targetUri.cleanUp();\r
489                 }\r
490         }\r
491 \r
492 }\r