else s.append(message); if (ex instanceof ServiceException) { for (Iterator t = ((ServiceException) ex).getExceptionText().iterator(); t.hasNext(); ) { s.append("\n");
public void testHandleServiceException() throws Exception { ServiceException exception = new ServiceException("hello service exception"); exception.setCode("helloCode"); exception.setLocator("helloLocator"); exception.getExceptionText().add("helloText"); handler.handleServiceException(exception, requestInfo); InputStream input = new ByteArrayInputStream(response.getContentAsString().getBytes()); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(true); Document doc = docBuilderFactory.newDocumentBuilder().parse(input); assertEquals("ows:ExceptionReport", doc.getDocumentElement().getNodeName()); }
public static ExceptionReportType exceptionReport( ServiceException exception, boolean verboseExceptions, String version) { ExceptionType e = f.createExceptionType(); if (exception.getCode() != null) { e.setExceptionCode(exception.getCode()); } else { // set a default e.setExceptionCode("NoApplicableCode"); } e.setLocator(exception.getLocator()); // add the message StringBuffer sb = new StringBuffer(); OwsUtils.dumpExceptionMessages(exception, sb, true); e.getExceptionText().add(sb.toString()); e.getExceptionText().addAll(exception.getExceptionText()); if (verboseExceptions) { // add the entire stack trace // exception. e.getExceptionText().add("Details:"); ByteArrayOutputStream trace = new ByteArrayOutputStream(); exception.printStackTrace(new PrintStream(trace)); e.getExceptionText().add(new String(trace.toByteArray())); } ExceptionReportType report = f.createExceptionReportType(); version = version != null ? version : "1.1.0"; report.setVersion(version); report.getException().add(e); return report; }
@SuppressWarnings("unchecked") public void testHandleServiceExceptionEncodingMore() throws Exception { String message1 = "foo & <foo> \"foo's\""; String message2 = "a \"different\" <message>"; ServiceException exception = new ServiceException(message1); exception.setLocator("test-locator"); exception.getExceptionText().add(message2); handler.handleServiceException(exception, requestInfo); InputStream input = new ByteArrayInputStream(response.getContentAsString().getBytes()); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(true); Document doc = docBuilderFactory.newDocumentBuilder().parse(input); Node exceptionText = XPathAPI.selectSingleNode( doc, "ows:ExceptionReport/ows:Exception/ows:ExceptionText/text()"); assertNotNull(exceptionText); String message = message1 + "\n" + message2; assertEquals( "round-tripped through character entities", message, exceptionText.getTextContent()); }
public void testHandleServiceExceptionCauses() throws Exception { // create a stack of three exceptions IllegalArgumentException illegalArgument = new IllegalArgumentException("Illegal argument here"); IOException ioException = new IOException("I/O exception here"); ioException.initCause(illegalArgument); ServiceException serviceException = new ServiceException("hello service exception"); serviceException.setCode("helloCode"); serviceException.setLocator("helloLocator"); serviceException.getExceptionText().add("helloText"); serviceException.initCause(ioException); handler.handleServiceException(serviceException, requestInfo); InputStream input = new ByteArrayInputStream(response.getContentAsString().getBytes()); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(true); Document doc = docBuilderFactory.newDocumentBuilder().parse(input); Node exceptionTextNode = XPathAPI.selectSingleNode( doc, "ows:ExceptionReport/ows:Exception/ows:ExceptionText/text()"); assertNotNull(exceptionTextNode); // normalise whitespace String exceptionText = exceptionTextNode.getNodeValue().replaceAll("\\s+", " "); assertTrue(exceptionText.indexOf(illegalArgument.getMessage()) != -1); assertTrue(exceptionText.indexOf(ioException.getMessage()) != -1); assertTrue(exceptionText.indexOf(serviceException.getMessage()) != -1); }
public void testHandleServiceExceptionNullMessages() throws Exception { // create a stack of three exceptions NullPointerException npe = new NullPointerException(); ServiceException serviceException = new ServiceException("hello service exception"); serviceException.setCode("helloCode"); serviceException.setLocator("helloLocator"); serviceException.getExceptionText().add("NullPointerException"); serviceException.initCause(npe); handler.handleServiceException(serviceException, requestInfo); InputStream input = new ByteArrayInputStream(response.getContentAsString().getBytes()); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(true); Document doc = docBuilderFactory.newDocumentBuilder().parse(input); Node exceptionTextNode = XPathAPI.selectSingleNode( doc, "ows:ExceptionReport/ows:Exception/ows:ExceptionText/text()"); assertNotNull(exceptionTextNode); // normalise whitespace String exceptionText = exceptionTextNode.getNodeValue().replaceAll("\\s+", " "); // used to contain an extra " null" at the end assertEquals("hello service exception NullPointerException", exceptionText); } }
OwsUtils.dumpExceptionMessages(exception, sb, false); e.getExceptionText().add(sb.toString()); e.getExceptionText().addAll(exception.getExceptionText());
public Object read(Object request, Reader reader, Map kvp) throws Exception { // TODO: make this configurable? configuration.getProperties().add(Parser.Properties.PARSE_UNKNOWN_ELEMENTS); Parser parser = new Parser(configuration); // set the input source with the correct encoding InputSource source = new InputSource(reader); source.setEncoding(wfs.getCharSet().name()); Object parsed = parser.parse(source); // valid request? this should definitley be a configuration option // TODO: HACK, disabling validation for transaction if (!"Transaction".equalsIgnoreCase(getElement().getLocalPart())) { if (!parser.getValidationErrors().isEmpty()) { WFSException exception = new WFSException("Invalid request", "InvalidParameterValue"); for (Iterator e = parser.getValidationErrors().iterator(); e.hasNext();) { Exception error = (Exception) e.next(); exception.getExceptionText().add(error.getLocalizedMessage()); } throw exception; } } return parsed; } }
if (exception.getExceptionText() != null && !exception.getExceptionText().isEmpty()) { sb.append("\n"); sb.append(exception.getExceptionText()); // check this
@SuppressWarnings("unchecked") public Object read(Object request, Reader reader, Map kvp) throws Exception { Parser parser = new Parser(configuration); parser.setValidating(true); parser.setFailOnValidationError(true); parser.setStrict(true); Object parsed; try { parsed = parser.parse(reader); } catch (Exception e) { throw new ServiceException("Could not parse XML request.", e); } if (!parser.getValidationErrors().isEmpty()) { ServiceException exception = new ServiceException("Invalid request", "InvalidParameterValue"); for (Exception error : (List<Exception>) parser.getValidationErrors()) { LOGGER.warning(error.getLocalizedMessage()); exception.getExceptionText().add(error.getLocalizedMessage()); } } return parsed; } }
exception.getExceptionText().add(error.getLocalizedMessage());
@SuppressWarnings("unchecked") public Object read(Object request, Reader reader, Map kvp) throws Exception { Parser parser = new Parser(configuration); parser.setValidating(true); parser.setFailOnValidationError(true); parser.setStrict(true); parser.setEntityResolver(resolverProvider.getEntityResolver()); Object parsed; try { parsed = parser.parse(reader); } catch (Exception e) { throw new ServiceException("Could not parse XML request.", e); } if (!parser.getValidationErrors().isEmpty()) { ServiceException exception = new ServiceException("Invalid request", "InvalidParameterValue"); for (Exception error : (List<Exception>) parser.getValidationErrors()) { LOGGER.warning(error.getLocalizedMessage()); exception.getExceptionText().add(error.getLocalizedMessage()); } } return parsed; } }
dumpExceptionMessages(exception, sb, true); e.getExceptionText().add(sb.toString()); e.getExceptionText().addAll(exception.getExceptionText());