18
18
import com .groupdocs .viewer .domain .options .DocumentInfoOptions ;
19
19
import com .groupdocs .viewer .domain .options .FileListOptions ;
20
20
import com .groupdocs .viewer .domain .options .RotatePageOptions ;
21
+ import com .groupdocs .viewer .exception .GroupDocsViewerException ;
22
+ import com .groupdocs .viewer .exception .InvalidPasswordException ;
21
23
import com .groupdocs .viewer .handler .ViewerHtmlHandler ;
22
24
import com .groupdocs .viewer .handler .ViewerImageHandler ;
23
25
import com .groupdocs .viewer .licensing .License ;
26
+ import com .groupdocs .viewer .localization .ILocalizationHandler ;
24
27
import io .dropwizard .jetty .ConnectorFactory ;
25
28
import io .dropwizard .jetty .HttpConnectorFactory ;
26
29
import io .dropwizard .server .SimpleServerFactory ;
@@ -150,7 +153,8 @@ public Object loadFileTree(@Context HttpServletRequest request, @Context HttpSer
150
153
}catch (Exception ex ){
151
154
// set exception message
152
155
ErrorMsgWrapper errorMsgWrapper = new ErrorMsgWrapper ();
153
- errorMsgWrapper .setError (ex .getMessage ());
156
+ errorMsgWrapper .setMessage (ex .getMessage ());
157
+ errorMsgWrapper .setException (ex );
154
158
return objectToJson (errorMsgWrapper );
155
159
}
156
160
}
@@ -166,31 +170,50 @@ public Object loadFileTree(@Context HttpServletRequest request, @Context HttpSer
166
170
public Object loadDocumentDescription (@ Context HttpServletRequest request , @ Context HttpServletResponse response ){
167
171
// set response content type
168
172
setResponseContentType (response , MediaType .APPLICATION_JSON );
173
+ String password = "" ;
169
174
try {
170
175
// get request body
171
176
String requestBody = getRequestBody (request );
172
177
// get/set parameters
173
178
String documentGuid = getJsonString (requestBody , "guid" );
174
179
boolean htmlMode = getJsonBoolean (requestBody , "htmlMode" );
180
+ password = getJsonString (requestBody , "password" );
175
181
// check if documentGuid contains path or only file name
176
182
if (!Paths .get (documentGuid ).isAbsolute ()){
177
183
documentGuid = quickViewConfig .getApplication ().getFilesDirectory () + "/" + documentGuid ;
178
184
}
179
185
DocumentInfoContainer documentInfoContainer = new DocumentInfoContainer ();
180
186
// get document info options
181
187
DocumentInfoOptions documentInfoOptions = new DocumentInfoOptions (documentGuid );
188
+ // set password for protected document
189
+ if (!password .isEmpty () && password != null ) {
190
+ documentInfoOptions .setPassword (password );
191
+ }
182
192
// get document info container
183
- if (htmlMode ) {
193
+ if (htmlMode ) {
184
194
documentInfoContainer = viewerHtmlHandler .getDocumentInfo (documentGuid , documentInfoOptions );
185
195
} else {
186
196
documentInfoContainer = viewerImageHandler .getDocumentInfo (documentGuid , documentInfoOptions );
187
197
}
188
198
// return document description
189
199
return objectToJson (documentInfoContainer .getPages ());
200
+ }catch (GroupDocsViewerException ex ){
201
+ // Set exception message
202
+ ErrorMsgWrapper errorMsgWrapper = new ErrorMsgWrapper ();
203
+ if (GroupDocsViewerException .class .isAssignableFrom (InvalidPasswordException .class ) && password .isEmpty ()) {
204
+ errorMsgWrapper .setMessage ("Password Required" );
205
+ }else if (GroupDocsViewerException .class .isAssignableFrom (InvalidPasswordException .class ) && !password .isEmpty ()){
206
+ errorMsgWrapper .setMessage ("Incorrect password" );
207
+ }else {
208
+ errorMsgWrapper .setMessage (ex .getMessage ());
209
+ }
210
+ errorMsgWrapper .setException (ex );
211
+ return objectToJson (errorMsgWrapper );
190
212
}catch (Exception ex ){
191
213
// set exception message
192
214
ErrorMsgWrapper errorMsgWrapper = new ErrorMsgWrapper ();
193
- errorMsgWrapper .setError (ex .getMessage ());
215
+ errorMsgWrapper .setMessage (ex .getMessage ());
216
+ errorMsgWrapper .setException (ex );
194
217
return objectToJson (errorMsgWrapper );
195
218
}
196
219
}
@@ -213,6 +236,7 @@ public Object loadDocumentPage(@Context HttpServletRequest request, @Context Htt
213
236
String documentGuid = getJsonString (requestBody , "guid" );
214
237
int pageNumber = getJsonInteger (requestBody , "page" );
215
238
boolean htmlMode = getJsonBoolean (requestBody , "htmlMode" );
239
+ String password = getJsonString (requestBody , "password" );
216
240
LoadedPageWrapper loadedPage = new LoadedPageWrapper ();
217
241
String angle = "0" ;
218
242
// set options
@@ -221,6 +245,10 @@ public Object loadDocumentPage(@Context HttpServletRequest request, @Context Htt
221
245
htmlOptions .setPageNumber (pageNumber );
222
246
htmlOptions .setCountPagesToRender (1 );
223
247
htmlOptions .setResourcesEmbedded (true );
248
+ // set password for protected document
249
+ if (!password .isEmpty () && password != null ) {
250
+ htmlOptions .setPassword (password );
251
+ }
224
252
// get page HTML
225
253
loadedPage .setPageHtml (viewerHtmlHandler .getPages (documentGuid , htmlOptions ).get (0 ).getHtmlContent ());
226
254
// get page rotation angle
@@ -229,6 +257,10 @@ public Object loadDocumentPage(@Context HttpServletRequest request, @Context Htt
229
257
ImageOptions imageOptions = new ImageOptions ();
230
258
imageOptions .setPageNumber (pageNumber );
231
259
imageOptions .setCountPagesToRender (1 );
260
+ // set password for protected document
261
+ if (!password .isEmpty ()) {
262
+ imageOptions .setPassword (password );
263
+ }
232
264
// get page image
233
265
byte [] bytes = IOUtils .toByteArray (viewerImageHandler .getPages (documentGuid , imageOptions ).get (0 ).getStream ());
234
266
// encode ByteArray into String
@@ -245,7 +277,8 @@ public Object loadDocumentPage(@Context HttpServletRequest request, @Context Htt
245
277
setResponseContentType (response , MediaType .APPLICATION_JSON );
246
278
// set exception message
247
279
ErrorMsgWrapper errorMsgWrapper = new ErrorMsgWrapper ();
248
- errorMsgWrapper .setError (ex .getMessage ());
280
+ errorMsgWrapper .setMessage (ex .getMessage ());
281
+ errorMsgWrapper .setException (ex );
249
282
return objectToJson (errorMsgWrapper );
250
283
}
251
284
}
@@ -269,6 +302,7 @@ public Object rotateDocumentPages(@Context HttpServletRequest request, @Context
269
302
int angle = Integer .parseInt (getJsonString (requestBody , "angle" ));
270
303
JSONArray pages = new JSONObject (requestBody ).getJSONArray ("pages" );
271
304
boolean htmlMode = getJsonBoolean (requestBody , "htmlMode" );
305
+ String password = getJsonString (requestBody , "password" );
272
306
// a list of the rotated pages info
273
307
ArrayList <RotatedPageWrapper > rotatedPages = new ArrayList <RotatedPageWrapper >();
274
308
// rotate pages
@@ -279,6 +313,10 @@ public Object rotateDocumentPages(@Context HttpServletRequest request, @Context
279
313
RotatePageOptions rotateOptions = new RotatePageOptions (pageNumber , angle );
280
314
// perform page rotation
281
315
String resultAngle = "0" ;
316
+ // set password for protected document
317
+ if (!password .isEmpty () && password != null ) {
318
+ rotateOptions .setPassword (password );
319
+ }
282
320
if (htmlMode ) {
283
321
viewerHtmlHandler .rotatePage (documentGuid , rotateOptions );
284
322
resultAngle = String .valueOf (viewerHtmlHandler .getDocumentInfo (documentGuid ).getPages ().get (pageNumber - 1 ).getAngle ());
@@ -299,7 +337,8 @@ public Object rotateDocumentPages(@Context HttpServletRequest request, @Context
299
337
setResponseContentType (response , MediaType .APPLICATION_JSON );
300
338
// set exception message
301
339
ErrorMsgWrapper errorMsgWrapper = new ErrorMsgWrapper ();
302
- errorMsgWrapper .setError (ex .getMessage ());
340
+ errorMsgWrapper .setMessage (ex .getMessage ());
341
+ errorMsgWrapper .setException (ex );
303
342
return objectToJson (errorMsgWrapper );
304
343
}
305
344
}
@@ -311,7 +350,7 @@ public Object rotateDocumentPages(@Context HttpServletRequest request, @Context
311
350
*/
312
351
@ GET
313
352
@ Path (value = "/downloadDocument" )
314
- public void downloadDocument (@ Context HttpServletRequest request , @ Context HttpServletResponse response ) throws ServletException , IOException {
353
+ public Object downloadDocument (@ Context HttpServletRequest request , @ Context HttpServletResponse response ) throws ServletException , IOException {
315
354
int bytesRead = 0 ;
316
355
int count = 0 ;
317
356
byte [] buff = new byte [16 * 1024 ];
@@ -329,9 +368,18 @@ public void downloadDocument(@Context HttpServletRequest request, @Context HttpS
329
368
// download the document
330
369
inputStream = new BufferedInputStream (new FileInputStream (documentGuid ));
331
370
outStream = new BufferedOutputStream (out );
332
- while ((count = inputStream .read (buff )) != -1 ) {
371
+ while ((count = inputStream .read (buff )) != -1 ) {
333
372
outStream .write (buff , 0 , count );
334
373
}
374
+ return outStream ;
375
+ } catch (Exception ex ){
376
+ // set response content type
377
+ setResponseContentType (response , MediaType .APPLICATION_JSON );
378
+ // set exception message
379
+ ErrorMsgWrapper errorMsgWrapper = new ErrorMsgWrapper ();
380
+ errorMsgWrapper .setMessage (ex .getMessage ());
381
+ errorMsgWrapper .setException (ex );
382
+ return objectToJson (errorMsgWrapper );
335
383
} finally {
336
384
// close streams
337
385
if (inputStream != null )
@@ -360,6 +408,8 @@ public Object uploadDocument(@Context HttpServletRequest request, @Context HttpS
360
408
Part filePart = request .getPart ("file" );
361
409
// get document URL
362
410
String documentUrl = request .getParameter ("url" );
411
+ // get rewrite mode
412
+ boolean rewrite = Boolean .parseBoolean (request .getParameter ("rewrite" ));
363
413
InputStream uploadedInputStream = null ;
364
414
String fileName = "" ;
365
415
if (documentUrl .isEmpty () || documentUrl == null ) {
@@ -376,7 +426,19 @@ public Object uploadDocument(@Context HttpServletRequest request, @Context HttpS
376
426
String documentStoragePath = quickViewConfig .getApplication ().getFilesDirectory ();
377
427
// save the file
378
428
File file = new File (documentStoragePath + "/" + fileName );
379
- Files .copy (uploadedInputStream , file .toPath (), StandardCopyOption .REPLACE_EXISTING );
429
+ // check rewrite mode
430
+ if (rewrite ) {
431
+ // save file with rewrite if exists
432
+ Files .copy (uploadedInputStream , file .toPath (), StandardCopyOption .REPLACE_EXISTING );
433
+ } else {
434
+ if (file .exists ())
435
+ {
436
+ // get file with new name
437
+ file = getFreeFileName (documentStoragePath , fileName );
438
+ }
439
+ // save file with out rewriting
440
+ Files .copy (uploadedInputStream , file .toPath ());
441
+ }
380
442
UploadedDocumentWrapper uploadedDocument = new UploadedDocumentWrapper ();
381
443
uploadedDocument .setGuid (documentStoragePath + "/" + fileName );
382
444
return objectToJson (uploadedDocument );
@@ -385,7 +447,8 @@ public Object uploadDocument(@Context HttpServletRequest request, @Context HttpS
385
447
setResponseContentType (response , MediaType .APPLICATION_JSON );
386
448
// set exception message
387
449
ErrorMsgWrapper errorMsgWrapper = new ErrorMsgWrapper ();
388
- errorMsgWrapper .setError (ex .getMessage ());
450
+ errorMsgWrapper .setMessage (ex .getMessage ());
451
+ errorMsgWrapper .setException (ex );
389
452
return objectToJson (errorMsgWrapper );
390
453
}
391
454
}
0 commit comments