Produced by Araxis Merge on 6/20/2018 10:24:21 AM Central Daylight Time. See www.araxis.com for information about Merge. This report uses XHTML and CSS2, and is best viewed with a modern standards-compliant browser. For optimum results when printing this report, use landscape orientation and enable printing of background images and colours in your browser.
# | Location | File | Last Modified |
---|---|---|---|
1 | CCRS.zip\CCRS\Reports-Sprint_5.zip\Reports-Sprint_5-aa03f94869cda45ee08ca75641e61a7376f2c2a5\CCRSBaseHelper\src\main\java\gov\va\ccrs\helpers | sFTPHelper.java | Fri Mar 30 15:47:12 2018 UTC |
2 | CCRS.zip\CCRS\Reports-Sprint_5.zip\Reports-Sprint_5-aa03f94869cda45ee08ca75641e61a7376f2c2a5\CCRSBaseHelper\src\main\java\gov\va\ccrs\helpers | sFTPHelper.java | Fri Jun 15 13:11:53 2018 UTC |
Description | Between Files 1 and 2 |
|
---|---|---|
Text Blocks | Lines | |
Unchanged | 2 | 634 |
Changed | 1 | 2 |
Inserted | 0 | 0 |
Removed | 0 | 0 |
Whitespace | |
---|---|
Character case | Differences in character case are significant |
Line endings | Differences in line endings (CR and LF characters) are ignored |
CR/LF characters | Not shown in the comparison detail |
No regular expressions were active.
1 | package go v.va.ccrs. helpers; | |
2 | ||
3 | import jav a.io.FileI nputStream ; | |
4 | import jav a.io.IOExc eption; | |
5 | import jav a.io.Input Stream; | |
6 | import jav a.nio.file .*; | |
7 | import jav a.nio.file .attribute .BasicFile Attributes ; | |
8 | import jav a.util.Pro perties; | |
9 | import jav a.util.Vec tor; | |
10 | ||
11 | import org .apache.lo g4j.Level; | |
12 | import com .jcraft.js ch.Channel ; | |
13 | import com .jcraft.js ch.Channel Sftp; | |
14 | import com .jcraft.js ch.JSch; | |
15 | import com .jcraft.js ch.JSchExc eption; | |
16 | import com .jcraft.js ch.Session ; | |
17 | import com .jcraft.js ch.SftpExc eption; | |
18 | ||
19 | /** | |
20 | * | |
21 | * Helper to retriev e a files from an sF TP Server | |
22 | * | |
23 | * @author pi i
|
|
24 | * @versio n 1 | |
25 | * @since 11/22/2017 | |
26 | * | |
27 | */ | |
28 | ||
29 | public cla ss sFTPHel per | |
30 | { | |
31 | privat e static J Sch _jsch = new JSch (); | |
32 | privat e static S tring _sft pServer; | |
33 | privat e static S tring _sft pUser; | |
34 | privat e static S tring _sft pPassw; | |
35 | privat e static S tring _sft pPath; | |
36 | privat e static S tring _tar getFolder; | |
37 | privat e static S tring _fil eExt; | |
38 | privat e static S tring _sft pPort; | |
39 | privat e static S tring _sft pArchiveFo lder; | |
40 | ||
41 | /** | |
42 | * | |
43 | * Sta tic Constr uctor Load sFTP conf iguration informatio n | |
44 | * | |
45 | */ | |
46 | static | |
47 | { | |
48 | Pr operties p rop = new Properties (); | |
49 | In putStream input = nu ll; | |
50 | ||
51 | tr y | |
52 | { | |
53 | input = new FileIn putStream( "config.pr operties") ; | |
54 | ||
55 | prop.loa d(input); | |
56 | ||
57 | _sftpSer ver = prop .getProper ty("sftpSe rver"); | |
58 | _sftpUse r = prop.g etProperty ("sftpUser "); | |
59 | _sftpPas sw = prop. getPropert y("sftpPas sword"); | |
60 | _sftpPat h = prop.g etProperty ("sftpPath "); | |
61 | _targetF older = pr op.getProp erty("targ etFolder") ; | |
62 | _fileExt = prop.ge tProperty( "Compressf ileExt"); | |
63 | _sftpPor t = prop.g etProperty ("sftpServ erPort"); | |
64 | _sftpArc hiveFolder = prop.ge tProperty( "sftpArchi veFolderPa th"); | |
65 | } | |
66 | ca tch (IOExc eption ex) | |
67 | { | |
68 | LogHelpe r.Log(Leve l.ERROR, e x); | |
69 | } | |
70 | fi nally | |
71 | { | |
72 | if (inpu t != null) | |
73 | { | |
74 | try | |
75 | { | |
76 | input.clos e(); | |
77 | } | |
78 | catc h (IOExcep tion e) | |
79 | { | |
80 | LogHelper. Log(Level. ERROR, e); | |
81 | } | |
82 | } | |
83 | } | |
84 | } | |
85 | ||
86 | /*** | |
87 | * | |
88 | * Clo se connect ion sessio n | |
89 | * | |
90 | * @pa ram sessio n | |
91 | */ | |
92 | privat e static v oid closeS ession(Ses sion sessi on) | |
93 | { | |
94 | se ssion.disc onnect(); | |
95 | } | |
96 | ||
97 | /** | |
98 | * | |
99 | * Ope n sFTP cha nnel | |
100 | * | |
101 | * @pa ram sessio n | |
102 | * @re turn | |
103 | * @th rows JSchE xception | |
104 | */ | |
105 | privat e static C hannel ope nChannel(S ession ses sion) thro ws JSchExc eption | |
106 | { | |
107 | Ch annel chan nel = sess ion.openCh annel("sft p"); | |
108 | ch annel.conn ect(); | |
109 | ||
110 | re turn (chan nel); | |
111 | } | |
112 | ||
113 | /** | |
114 | * | |
115 | * Clo se sFTP ch annel | |
116 | * | |
117 | * @pa ram channe l | |
118 | * @th rows JSchE xception | |
119 | */ | |
120 | privat e static v oid closeC hannel(Cha nnel chann el) throws JSchExcep tion | |
121 | { | |
122 | ch annel.disc onnect(); | |
123 | } | |
124 | ||
125 | /** | |
126 | * | |
127 | * Mov es the dow nload file into the acrhive fo lder (if t here is on e) | |
128 | * | |
129 | * @pa ram fileNa me | |
130 | * @pa ram sftpCh annel | |
131 | * @th rows SftpE xception | |
132 | */ | |
133 | ||
134 | privat e static v oid archiv eFiles(Str ing fileNa me, Channe lSftp sftp Channel) t hrows Sftp Exception | |
135 | { | |
136 | if (_sftpArch iveFolder. trim().len gth() > 0 && _sftpAr chiveFolde r != _sftp Path && !_ sftpArchiv eFolder.eq uals("/")) | |
137 | { | |
138 | String r emoteFolde r = sftpCh annel.getH ome(); | |
139 | ||
140 | sftpChan nel.cd(rem oteFolder + (remoteF older.ends With("/") ? "" : "/" ) + _sftp Path); | |
141 | ||
142 | if (sftp Channel.ge t(fileName ) != null) | |
143 | { | |
144 | sftp Channel.re name(remot eFolder + (remoteFol der.endsWi th("/")?"" : "/") + _ sftpPath + fileName, remoteFol der + (rem oteFolder. endsWith(" /")?"": "/ ") + _sftp ArchiveFol der + file Name ); | |
145 | // s ftpChannel .cd(remote Folder + ( remoteFold er.endsWit h("/")?"": "/") + _s ftpPath); | |
146 | // s ftpChannel .rm(remote Folder + ( remoteFold er.endsWit h("/")?"": "/") + _s ftpPath + fileName); | |
147 | } | |
148 | } | |
149 | } | |
150 | ||
151 | /** | |
152 | * | |
153 | * Cas t opened c hannel to a sFTP Cha nnel | |
154 | * | |
155 | * @pa ram channe l | |
156 | * @re turn | |
157 | * @th rows SftpE xception | |
158 | */ | |
159 | privat e static C hannelSftp opensFTPC hannel(Cha nnel chann el) throws SftpExcep tion | |
160 | { | |
161 | Ch annelSftp sftpChanne l = (Chann elSftp) ch annel; | |
162 | ||
163 | re turn (sftp Channel); | |
164 | } | |
165 | ||
166 | /** | |
167 | * | |
168 | * Get all files in sFTP S erver and move them to a local folder | |
169 | * | |
170 | * @pa ram sftpCh annel | |
171 | * @th rows SftpE xception | |
172 | */ | |
173 | privat e static v oid getAll sFTPFiles( ChannelSft p sftpChan nel) throw s SftpExce ption | |
174 | { | |
175 | if (!_sftpPat h.trim().e quals("/") ) | |
176 | { | |
177 | sftpChan nel.cd(_sf tpPath); | |
178 | } | |
179 | ||
180 | @S uppressWar nings("unc hecked") | |
181 | Ve ctor<Chann elSftp.LsE ntry> list = sftpCha nnel.ls("* " + _fileE xt); | |
182 | ||
183 | fo r (Channel Sftp.LsEnt ry entry : list) | |
184 | { | |
185 | // downl oad file i nto local folder | |
186 | sftpChan nel.get(en try.getFil ename(), _ targetFold er + entry .getFilena me()); | |
187 | ||
188 | // move file to Ar chive fold er | |
189 | archiveF iles(entry .getFilena me(), sftp Channel); // | |
190 | } | |
191 | } | |
192 | ||
193 | /** | |
194 | * | |
195 | * Exi t sFTP Cha nnel | |
196 | * | |
197 | * @pa ram sftpCh annel | |
198 | */ | |
199 | privat e static v oid exitsF TPChannel( ChannelSft p sftpChan nel) | |
200 | { | |
201 | sf tpChannel. exit(); | |
202 | } | |
203 | ||
204 | /** | |
205 | * | |
206 | * Rem ove all fi les in the local fol der before retrievin g the new ones from the sFTP s erver | |
207 | * | |
208 | * @th rows IOExc eption | |
209 | */ | |
210 | privat e static v oid remove FilesFromL ocalFolder () throws IOExceptio n | |
211 | { | |
212 | Pa th path = Paths.get( _targetFol der); | |
213 | ||
214 | if (Files.exi sts(path)) | |
215 | { | |
216 | Files.wa lkFileTree (path, new SimpleFil eVisitor<P ath>() | |
217 | { | |
218 | @Ove rride | |
219 | publ ic FileVis itResult v isitFile(P ath file, BasicFileA ttributes attrs) thr ows IOExce ption | |
220 | { | |
221 | Files.dele te(file); | |
222 | return Fil eVisitResu lt.CONTINU E; | |
223 | } | |
224 | }); | |
225 | } | |
226 | el se | |
227 | { | |
228 | Files.cr eateDirect ory(path); | |
229 | } | |
230 | } | |
231 | ||
232 | /** | |
233 | * | |
234 | * Ope n Session connection | |
235 | * | |
236 | */ | |
237 | privat e static S ession ope nSftpConne ction() th rows JSchE xception | |
238 | { | |
239 | if ((_sftpUs er != null && _sftpS erver != n ull && _sf tpPassw != null) | |
240 | && ( _sftpUser. trim().len gth() > 0 && _sftpSe rver.trim( ).length() > 0 && _s ftpPassw.t rim().leng th() > 0)) | |
241 | { | |
242 | Session session = _jsch.getS ession(_sf tpUser, _s ftpServer) ; | |
243 | session. setPasswor d(_sftpPas sw); | |
244 | ||
245 | java.uti l.Properti es config = new java .util.Prop erties(); | |
246 | config.p ut("Strict HostKeyChe cking", "n o"); | |
247 | session. setConfig( config); | |
248 | ||
249 | if (_sft pPort.trim ().length( ) > 0) | |
250 | { | |
251 | sess ion.setPor t(Integer. parseInt(_ sftpPort)) ; | |
252 | } | |
253 | ||
254 | session. connect(); | |
255 | ||
256 | return ( session); | |
257 | } | |
258 | el se | |
259 | { | |
260 | throw ne w JSchExce ption("The sFTP User , Password or Server name can' t Empty"); | |
261 | } | |
262 | } | |
263 | ||
264 | /** | |
265 | * | |
266 | * Pub lic method that gets and saves in a loca l folder t he files i n an sFTP | |
267 | * ser ver | |
268 | * | |
269 | */ | |
270 | public static vo id getsFTP Files() | |
271 | { | |
272 | Se ssion sess ion = null ; | |
273 | ||
274 | tr y | |
275 | { | |
276 | // remov e files fr om temp fo lder | |
277 | removeFi lesFromLoc alFolder() ; | |
278 | ||
279 | // Open sFTP Sessi on | |
280 | session = openSftp Connection (); | |
281 | ||
282 | // Open Channel | |
283 | Channel channel = openChanne l(session) ; | |
284 | ||
285 | // Cast Channel to sFTPChann el | |
286 | ChannelS ftp sftpCh annel = op ensFTPChan nel(channe l); | |
287 | ||
288 | // Get a ll files i n sFTP ser ver | |
289 | getAllsF TPFiles(sf tpChannel) ; | |
290 | ||
291 | // Exit sFTP Chann el | |
292 | exitsFTP Channel(sf tpChannel) ; | |
293 | ||
294 | // Close Open Chan nel | |
295 | closeCha nnel(chann el); | |
296 | ||
297 | } | |
298 | ca tch( IOExc eption ex) | |
299 | { | |
300 | LogHelpe r.Log(Leve l.ERROR, e x); | |
301 | } | |
302 | ca tch (JSchE xception e x) | |
303 | { | |
304 | LogHelpe r.Log(Leve l.ERROR, e x); | |
305 | } | |
306 | ca tch (SftpE xception e x) | |
307 | { | |
308 | LogHelpe r.Log(Leve l.ERROR, e x); | |
309 | } | |
310 | fi nally | |
311 | { | |
312 | if (sess ion != nul l && sessi on.isConne cted()) | |
313 | { | |
314 | clos eSession(s ession); | |
315 | } | |
316 | } | |
317 | } | |
318 | } |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.