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