public void checkMaxTimes(Set result, int maxValues) { // limiting number of elements we can create if (maxValues > 0 && result.size() > maxValues) { throw new ServiceException( "More than " + maxValues + " times specified in the request, bailing out.", ServiceException.INVALID_PARAMETER_VALUE, "time"); } }
public void checkMaxElevations(Set result, int maxValues) { // limiting number of elements we can create if (maxValues > 0 && result.size() > maxValues) { throw new ServiceException( "More than " + maxValues + " elevations specified in the request, bailing out.", ServiceException.INVALID_PARAMETER_VALUE, "elevation"); } }
/** * Checks the validity of a version number (the specification version numbers, three dot * separated integers between 0 and 99). Throws a ServiceException if the version number is not * valid. * * @param v the version number (in string format) * @param locator The locator for the service exception (may be null) */ public static void checkVersionNumber(String v, String locator) throws ServiceException { if (!v.matches("[0-9]{1,2}\\.[0-9]{1,2}\\.[0-9]{1,2}")) { String msg = v + " is an invalid version number"; throw new ServiceException(msg, "VersionNegotiationFailed", locator); } }
public Object parse(String value) throws Exception { try { return XCQL.toFilterList(value); } catch (CQLException pe) { throw new ServiceException("Could not parse CQL filter list.", pe); } } }
@Transactional public class PeopleService { .... private PeopleDAO pDAO; private CityDAO cDAO; ... public void createPerson(String name, String city) throws PeopleServiceException { Person p = new Person(); p.setName(name); City c = cDAO.getCityByName(city); if (c == null) throw new ServiceException(city + " doesn't exist!"); if (c.isFull()) throw new ServiceException(city + " is full!"); c.addPeople(p); sess().save(p); sess().save(c); } ... }
public Filter filter() { try { if (filter == null && cqlFilter != null && !cqlFilter.isEmpty()) { filter = ECQL.toFilter(cqlFilter); } } catch (CQLException e) { throw new org.geoserver.platform.ServiceException( "Failed to generate filter from ECQL string " + e.getMessage()); } return filter; }
public void write(Object value, OutputStream output, Operation operation) throws IOException { Message message = (Message) value; output.write(message.message.getBytes()); throw new ServiceException("Whoops, something gone wrong!"); }
/** * Parses a CQL filter * * @param filter * @throws ServiceException * @deprecated use {@link CQLFilterKvpParser} */ protected List readCQLFilter(String filter) throws ServiceException { try { return XCQL.toFilterList(filter); } catch (CQLException pe) { throw new ServiceException("Could not parse CQL filter list." + pe.getMessage(), pe); } }
public Object read(Object request, Map kvp, Map rawKvp) throws Exception { // use emf reflection EObject eObject = (EObject) request; for (Iterator e = kvp.entrySet().iterator(); e.hasNext(); ) { Map.Entry entry = (Map.Entry) e.next(); String property = (String) entry.getKey(); Object value = entry.getValue(); // respect the filter if (filter(property)) { continue; } if (EMFUtils.has(eObject, property)) { try { setValue(eObject, property, value); } catch (Exception ex) { throw new ServiceException( "Failed to set property " + property + " in request object using value " + value + (value != null ? " of type " + value.getClass() : ""), ex, ServiceException.INVALID_PARAMETER_VALUE, property); } } } return request; }
/** * Constructs a {@link WFSDataStore} from an OWS URL. * * @param remoteOwsUrl * @return * @throws ServiceException if there was a problem accessing the remote service */ protected static DataStore connectRemoteWFS(URL remoteOwsUrl) { try { WFSDataStoreFactory storeFactory = new WFSDataStoreFactory(); Map params = new HashMap(storeFactory.getImplementationHints()); params.put( WFSDataStoreFactory.URL.key, remoteOwsUrl + "&request=GetCapabilities&service=WFS"); params.put(WFSDataStoreFactory.TRY_GZIP.key, Boolean.TRUE); DataStore dataStore = storeFactory.createDataStore(params); return dataStore; } catch (Exception e) { throw new ServiceException("Could not connect to remote OWS", e, "RemoteOWSFailure"); } }
public void wrappedHttpErrorCodeException() { try { throw new HttpErrorCodeException(HttpServletResponse.SC_NO_CONTENT); } catch (Exception e) { throw new ServiceException("Wrapping code error", e); } }
private MockHttpServletResponse getAsServletResponse(String path, int statusCode) throws Exception { MockHttpServletResponse response = getAsServletResponse(path); int status = response.getStatus(); if (statusCode != status) { String content = response.getContentAsString(); if (content == null || content.length() == 0) { throw new ServiceException( "expected status <" + statusCode + "> but was <" + status + ">"); } else { throw new ServiceException( "expected status <" + statusCode + "> but was <" + status + ">:" + content); } } return response; }
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()); }
@Override public PublishedInfo visitNamedLayerInternal(StyledLayer namedLayer) { PublishedInfo p = catalog.getLayerGroupByName(namedLayer.getName()); if (p == null) { p = catalog.getLayerByName(namedLayer.getName()); if (p == null) { throw new ServiceException( "No layer or layer group with name \"" + namedLayer.getName() + "\""); } } return p; }
/** * Executes an ows request using the GET method and returns the result as an JSON document. * * @param path The portion of the request after the context, example: * 'wms?request=GetMap&version=1.1.1&..." * @param statusCode Expected status code * @return A result of the request parsed into a dom. */ protected JSON getAsJSON(final String path, int statusCode) throws Exception { MockHttpServletResponse response = getAsServletResponse(path, statusCode); int status = response.getStatus(); if (statusCode != status) { String content = response.getContentAsString(); if (content == null || content.length() == 0) { throw new ServiceException( "expected status <" + statusCode + "> but was <" + status + ">"); } else { throw new ServiceException( "expected status <" + statusCode + "> but was <" + status + ">:" + content); } } return json(response); }
public void testHandleServiceExceptionEncoding() throws Exception { String message = "foo & <foo> \"foo's\""; ServiceException exception = new ServiceException(message); exception.setLocator("test-locator"); 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); assertEquals( "round-tripped through character entities", message, exceptionText.getTextContent()); }
@Override public void visit(NamedLayer layer) { if (null == layer.getName()) { throw new ServiceException("A UserLayer or NamedLayer without layer name was passed"); } setLayerState(visitNamedLayerInternal(layer), layer); if (!handleLayerGroup()) { super.visit(layer); } handleNoStyles(); clearLayerState(); }
@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); } }