ProcessDocumentServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { String templateId = FileServletUtil.encodeFileNamePart(request.getParameter("templateid")); String docId = FileServletUtil.encodeFileNamePart(request.getParameter("docid")); if (templateId == null || docId == null) { logger.error(MessageFormat.format(INVALID_PARAMS, templateId, docId)); return; } KieServicesClient kieServicesClient = kieServerIntegration.getServerClient(templateId); if (kieServicesClient != null) { DocumentServicesClient documentServicesClient = kieServicesClient.getServicesClient(DocumentServicesClient.class); DocumentInstance documentInstance = documentServicesClient.getDocument(docId); if (documentInstance != null) { response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=\"" + documentInstance.getName() + "\""); response.getOutputStream().write(documentInstance.getContent()); } else { logger.error(MessageFormat.format(INVALID_DOCUMENT, docId)); } } else { logger.error(MessageFormat.format(INVALID_SERVICES_CLIENT, templateId)); return; } } catch (Exception e) { logger.error(MessageFormat.format(ERROR_RETRIEVING_DOC, e.getMessage())); return; } } ProcessDocumentServlet(); void setKieServerIntegration(KieServerIntegration kieServerIntegration); static final String ERROR_RETRIEVING_DOC; static final String INVALID_SERVICES_CLIENT; static final String INVALID_DOCUMENT; static final String INVALID_PARAMS; }
@Test public void testInvalidRequestParamets() throws Exception { HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); when(request.getParameter("templateid")).thenReturn(null); when(request.getParameter("docid")).thenReturn(null); ProcessDocumentServlet processDocServlet = new ProcessDocumentServlet(); processDocServlet.doGet(request, response); verify(loggingAppender).doAppend(loggingEventArgumentCaptor.capture()); final LoggingEvent loggingEvent = loggingEventArgumentCaptor.getValue(); assertEquals(Level.ERROR, loggingEvent.getLevel()); assertEquals(MessageFormat.format(ProcessDocumentServlet.INVALID_PARAMS, "null", "null"), loggingEvent.getFormattedMessage()); }