SxmpServletProcessor { public static void doProcess(SxmpProcessor processor, InputStream in, PrintWriter out, String uri, String method, String contentType) throws IOException, HttpStatusCodeException { int posOfVersion = uri.lastIndexOf('/'); if (posOfVersion < 0 || posOfVersion+1 >= uri.length()) { throw new HttpStatusCodeException(HttpServletResponse.SC_NOT_FOUND, "Bad URL used, could not extract version or no data for version"); } String version = uri.substring(posOfVersion+1); if (!version.equals(SxmpParser.VERSION_1_0) && !version.equals(SxmpParser.VERSION_1_1)) { throw new HttpStatusCodeException(HttpServletResponse.SC_BAD_REQUEST, "Unsupported API version in URL"); } if (!method.equalsIgnoreCase("POST")) { throw new HttpStatusCodeException(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "Only HTTP POST methods are acceptable"); } if (contentType == null || !contentType.toLowerCase().startsWith("text/xml")) { throw new HttpStatusCodeException(HttpServletResponse.SC_BAD_REQUEST, "Unsupported Content-Type HTTP Header - Must Be text/xml"); } SxmpSession session = new SxmpSession(processor, version); Response response = null; try { response = session.process(in); } catch (Exception e) { throw new HttpStatusCodeException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); } if (response == null) { logger.error(fatal, "The response from SxmpSession.process() was null -- should be impossible"); throw new HttpStatusCodeException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Response was empty"); } try { SxmpWriter.write(out, response); } catch (SxmpErrorException e) { logger.error("Error while writing response", e); throw new HttpStatusCodeException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Unable to cleanly write response to OutputStream"); } } SxmpServletProcessor(); void process(SxmpProcessor processor, HttpServletRequest request, HttpServletResponse response); static void doProcess(SxmpProcessor processor, InputStream in, PrintWriter out, String uri, String method, String contentType);  }
@Test public void doProcessBadAPIVersion() throws Exception { SxmpServletProcessor processor = new SxmpServletProcessor(); try { processor.doProcess(null, null, null, "/any/path/to/servlet/2.0", "POST", "text/xml"); } catch (HttpStatusCodeException e) { Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, e.getStatusCode()); Assert.assertThat(e.getMessage(), CoreMatchers.containsString("Unsupported API version")); } }
@Test public void doProcessBadMethod() throws Exception { SxmpServletProcessor processor = new SxmpServletProcessor(); try { processor.doProcess(null, null, null, "/api/sxmp/1.0", "GET", "text/xml"); } catch (HttpStatusCodeException e) { Assert.assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getStatusCode()); Assert.assertThat(e.getMessage(), CoreMatchers.containsString("Only HTTP POST method")); } }
@Test public void doProcessBadURL() throws Exception { SxmpServletProcessor processor = new SxmpServletProcessor(); try { processor.doProcess(null, null, null, "/", "POST", "text/xml"); } catch (HttpStatusCodeException e) { Assert.assertEquals(HttpStatus.SC_NOT_FOUND, e.getStatusCode()); Assert.assertThat(e.getMessage(), CoreMatchers.containsString("Bad URL used")); } }
@Test public void doProcessNonXMLContentTypeHeader() throws Exception { SxmpServletProcessor processor = new SxmpServletProcessor(); try { processor.doProcess(null, null, null, "/1.0", "POST", "text/html"); } catch (HttpStatusCodeException e) { Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, e.getStatusCode()); Assert.assertThat(e.getMessage(), CoreMatchers.containsString("Unsupported Content-Type")); } }
@Test public void doProcessSubmitWithSxmpErrorThrown() throws Exception { StringBuilder string0 = new StringBuilder(200) .append("<?xml version=\"1.0\"?>\n") .append("<operation type=\"submit\">\n") .append(" <account username=\"customer1\" password=\"test1\"/>\n") .append(" <submitRequest referenceId=\"MYREF102020022\">\n") .append(" <operatorId>10</operatorId>\n") .append(" <deliveryReport>true</deliveryReport>\n") .append(" <sourceAddress type=\"network\">40404</sourceAddress>\n") .append(" <destinationAddress type=\"international\">+12065551212</destinationAddress>\n") .append(" <text encoding=\"ISO-8859-1\">48656c6c6f20576f726c64</text>\n") .append(" </submitRequest>\n") .append("</operation>\n") .append(""); ByteArrayInputStream is = new ByteArrayInputStream(string0.toString().getBytes()); StringWriter sw = new StringWriter(); SxmpProcessor requestProcessor = new SxmpProcessor() { public boolean authenticate(Account account) throws SxmpErrorException { return true; } public SubmitResponse submit(Account account, SubmitRequest submitReq) throws SxmpErrorException { throw new SxmpErrorException(SxmpErrorCode.INVALID_VALUE, "Short code not allowed"); } public DeliverResponse deliver(Account account, DeliverRequest deliverRequest) throws SxmpErrorException { throw new UnsupportedOperationException("Not supported yet."); } public DeliveryReportResponse deliveryReport(Account account, DeliveryReportRequest deliveryRequest) throws SxmpErrorException { throw new UnsupportedOperationException("Not supported yet."); } }; SxmpServletProcessor processor = new SxmpServletProcessor(); processor.doProcess(requestProcessor, is, new PrintWriter(sw), "/1.0", "POST", "text/xml; charset=ISO-8859-1"); logger.debug(sw.toString()); }
@Test public void doProcessDeliverWithUnsupportedSxmpErrorThrown() throws Exception { StringBuilder string0 = new StringBuilder(200) .append("<?xml version=\"1.0\"?>\n") .append("<operation type=\"deliver\">\n") .append(" <account username=\"customer1\" password=\"test1\"/>\n") .append(" <deliverRequest referenceId=\"MYREF102020022\">\n") .append(" <operatorId>10</operatorId>\n") .append(" <sourceAddress type=\"network\">40404</sourceAddress>\n") .append(" <destinationAddress type=\"international\">+12065551212</destinationAddress>\n") .append(" <text encoding=\"ISO-8859-1\">48656c6c6f20576f726c64</text>\n") .append(" </deliverRequest>\n") .append("</operation>\n") .append(""); ByteArrayInputStream is = new ByteArrayInputStream(string0.toString().getBytes()); StringWriter sw = new StringWriter(); SxmpProcessor requestProcessor = new SxmpProcessor() { public boolean authenticate(Account account) throws SxmpErrorException { return true; } public SubmitResponse submit(Account account, SubmitRequest submitReq) throws SxmpErrorException { throw new SxmpErrorException(SxmpErrorCode.INVALID_VALUE, "Short code not allowed"); } public DeliverResponse deliver(Account account, DeliverRequest deliverRequest) throws SxmpErrorException { throw new SxmpErrorException(SxmpErrorCode.INVALID_VALUE, "Short code not allowed"); } public DeliveryReportResponse deliveryReport(Account account, DeliveryReportRequest deliveryRequest) throws SxmpErrorException { throw new UnsupportedOperationException("Not supported yet."); } }; new SxmpServletProcessor().doProcess(requestProcessor, is, new PrintWriter(sw), "/1.0", "POST", "text/xml; charset=ISO-8859-1"); StringBuilder expectedXML = new StringBuilder(200) .append("<?xml version=\"1.0\" encoding=\"utf-8\"?><operation type=\"deliver\"><error code=\"1006\" message=\"Short code not allowed\"/></operation>"); XMLUnit.setIgnoreWhitespace(true); Diff myDiff = new Diff(expectedXML.toString(), sw.toString()); DetailedDiff myDetailedDiff = new DetailedDiff(myDiff); Assert.assertTrue("Deliver XML are similar " + myDetailedDiff, myDetailedDiff.similar()); }