Skip to content

Commit 026c590

Browse files
committed
more consistent behaviour of PDFGenerator responses.
Returns redirect from POST always if accept is JSON. Sets HTTP status codes like 404 also for non-JSON responses.
1 parent a576673 commit 026c590

1 file changed

Lines changed: 40 additions & 15 deletions

File tree

pdf/src/main/java/digilib/servlet/PDFGenerator.java

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,23 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
152152
notifyUser(PDFStatus.ERROR, "[missing docid]", request, response);
153153
return;
154154
}
155-
docid = decodeDocid(docid);
155+
try {
156+
docid = decodeDocid(docid);
157+
} catch (Exception e) {
158+
notifyUser(PDFStatus.ERROR, "[invalid docid]", request, response);
159+
return;
160+
}
156161

157162
PDFStatus status = getStatus(docid);
158163
if (status == PDFStatus.NONEXISTENT) {
159-
// no file -- should not have happened
164+
// no file -- should not happen
160165
logger.error("Nonexistent file for docid!");
161166
notifyUser(PDFStatus.ERROR, docid, request, response);
162167
return;
163168

164169
} else if (status == PDFStatus.DONE) {
165170
// pdf created
166-
if ("application/json".equalsIgnoreCase(request.getHeader("Accept"))) {
171+
if (isJsonRequest(request)) {
167172
// send json status
168173
notifyUser(status, docid, request, response);
169174
return;
@@ -208,31 +213,32 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr
208213
// create and check PDF request (may throw exception)
209214
PDFRequest pdfji = new PDFRequest(request, dlConfig);
210215
docid = pdfji.getDocumentId();
216+
// status URL for this document
217+
String statusUrl = "?docid=" + encodeDocid(docid);
211218

212219
// if some invalid data has been entered ...
213220
if (!pdfji.isValid()) {
214221
notifyUser(PDFStatus.ERROR, docid, request, response);
215222
return;
216223
}
217-
224+
225+
// check current status
218226
PDFStatus status = getStatus(docid);
219227

220228
if (status == PDFStatus.NONEXISTENT) {
221229
// PDF not there -- start creation
222230
try {
223231
// start PDF creation thread
224232
createNewPdfDocument(pdfji, docid);
225-
// redirect client with docid parameter
226-
String url = "?docid=" + encodeDocid(docid);
227-
logger.debug("redirecting to {}", url);
228-
response.sendRedirect(url);
233+
// redirect client to status
234+
logger.debug("redirecting to {}", statusUrl);
235+
response.sendRedirect(statusUrl);
229236
return;
230237
} catch (FileAlreadyExistsException e) {
231238
// temp file actually exists - assume WIP
232239
logger.warn("Temp file seems to exist: {} - assume WIP.", e.getMessage());
233-
String url = "?docid=" + encodeDocid(docid);
234-
logger.debug("redirecting to {}", url);
235-
response.sendRedirect(url);
240+
logger.debug("redirecting to {}", statusUrl);
241+
response.sendRedirect(statusUrl);
236242
return;
237243
} catch (IOException e) {
238244
// error in pdf creation
@@ -242,7 +248,14 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr
242248
}
243249

244250
} else if (status == PDFStatus.DONE) {
245-
// PDF created -- send it
251+
// PDF has been created
252+
if (isJsonRequest(request)) {
253+
// redirect client to status
254+
logger.debug("redirecting to {}", statusUrl);
255+
response.sendRedirect(statusUrl);
256+
return;
257+
}
258+
// send the file
246259
try {
247260
logger.debug("PDF docid={} already DONE", docid);
248261
ServletOps.sendFile(getCacheFile(docid), "application/pdf", getDownloadFilename(pdfji), response,
@@ -255,8 +268,9 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr
255268
}
256269

257270
} else {
258-
// should be PDF in progress
259-
notifyUser(status, docid, request, response);
271+
// other status like in progress - redirect client to status
272+
logger.debug("redirecting to {}", statusUrl);
273+
response.sendRedirect(statusUrl);
260274
return;
261275
}
262276
} catch (IOException e) {
@@ -323,7 +337,7 @@ public void notifyUser(PDFStatus status, String documentid, HttpServletRequest r
323337
}
324338

325339
try {
326-
if ("application/json".equalsIgnoreCase(request.getHeader("Accept"))) {
340+
if (isJsonRequest(request)) {
327341
/*
328342
* REST style answer with JSON content
329343
*/
@@ -343,6 +357,7 @@ public void notifyUser(PDFStatus status, String documentid, HttpServletRequest r
343357
/*
344358
* browser style forward to the relevant jsp
345359
*/
360+
response.setStatus(httpStatus);
346361
ServletContext context = getServletContext();
347362
RequestDispatcher dispatch = context.getRequestDispatcher(nextPage);
348363
dispatch.forward(request, response);
@@ -470,4 +485,14 @@ public String encodeDocid(String docid) {
470485
public String decodeDocid(String encid) {
471486
return new String(Base64.getUrlDecoder().decode(encid));
472487
}
488+
489+
/**
490+
* Returns if the request asked for a JSON response.
491+
* @param request
492+
* @return
493+
*/
494+
protected boolean isJsonRequest(HttpServletRequest request) {
495+
return "application/json".equalsIgnoreCase(request.getHeader("Accept"));
496+
}
497+
473498
}

0 commit comments

Comments
 (0)