private static String getCallbackFunction() { Request request = Dispatcher.REQUEST.get(); if (request == null) { return JSONType.CALLBACK_FUNCTION; } else { return JSONType.getCallbackFunction(request.getKvp()); } }
/** capabilities output format string. */ public String getCapabilitiesElementName() { return JSONType.getJSONType(getOutputFormat()).toString(); }
/** * get an array containing all the MimeType handled by this object * * @return return a string array of handled MimeType */ public static String[] getSupportedTypes() { if (isJsonpEnabled()) return new String[] {json, simple_json, jsonp}; else return new String[] {json, simple_json}; }
/** * Check if the passed MimeType is a valid jsonp and if jsonp is enabled * * @param type the MimeType string representation to check * @return true if type is equalsIgnoreCase to {@link #jsonp} and jsonp is enabled * @see {@link JSONType#isJsonMimeType(String)} */ public static boolean useJsonp(String type) { return JSONType.isJsonpEnabled() && JSONType.isJsonpMimeType(type); }
private static void writeJsonpException( ServiceException exception, Request request, OutputStream out, String charset, boolean verbose) throws IOException { OutputStreamWriter outWriter = new OutputStreamWriter(out, charset); final String callback; if (request == null) { callback = JSONType.CALLBACK_FUNCTION; } else { callback = JSONType.getCallbackFunction(request.getKvp()); } outWriter.write(callback + "("); writeJsonException(exception, request, outWriter, verbose); outWriter.write(")"); outWriter.flush(); IOUtils.closeQuietly(outWriter); }
/** Encodes a ogc:ServiceExceptionReport to output. */ public void handleServiceException(ServiceException exception, Request request) { boolean verbose = gs.getSettings().isVerboseExceptions(); String charset = gs.getSettings().getCharset(); // first of all check what kind of exception handling we must perform final String exceptions; try { exceptions = (String) request.getKvp().get("EXCEPTIONS"); if (exceptions == null) { // use default handleDefault(exception, request, charset, verbose); return; } } catch (Exception e) { // width and height might be missing handleDefault(exception, request, charset, verbose); return; } if (JSONType.isJsonMimeType(exceptions)) { // use Json format JSONType.handleJsonException(LOGGER, exception, request, charset, verbose, false); } else if (JSONType.useJsonp(exceptions)) { // use JsonP format JSONType.handleJsonException(LOGGER, exception, request, charset, verbose, true); } else { handleDefault(exception, request, charset, verbose); } }
@Test public void testMimeType() { // MimeType assertNotSame(JSONType.json, JSONType.jsonp); assertTrue(JSONType.isJsonMimeType(JSONType.json)); // enable JsonP programmatically JSONType.setJsonpEnabled(true); // check jsonp is enabled assertTrue(JSONType.useJsonp(JSONType.jsonp)); // disable JsonP JSONType.setJsonpEnabled(false); assertFalse(JSONType.useJsonp(JSONType.jsonp)); }
@Test public void testExceptions() throws Exception { Document doc = getAsDOM("wms?service=WMS&request=getCapabilities&version=1.1.1", true); XpathEngine xpath = XMLUnit.newXpathEngine(); assertTrue( xpath.evaluate("//Exception/Format[1]", doc).equals("application/vnd.ogc.se_xml")); assertTrue( xpath.evaluate("//Exception/Format[2]", doc) .equals("application/vnd.ogc.se_inimage")); assertTrue( xpath.evaluate("//Exception/Format[3]", doc) .equals("application/vnd.ogc.se_blank")); assertTrue(xpath.evaluate("//Exception/Format[4]", doc).equals("application/json")); assertTrue(xpath.getMatchingNodes("//Exception/Format", doc).getLength() >= 4); boolean jsonpOriginal = JSONType.isJsonpEnabled(); try { JSONType.setJsonpEnabled(true); doc = getAsDOM("wms?service=WMS&request=getCapabilities&version=1.1.1", true); assertTrue(xpath.evaluate("//Exception/Format[5]", doc).equals("text/javascript")); assertTrue(xpath.getMatchingNodes("//Exception/Format", doc).getLength() == 5); JSONType.setJsonpEnabled(false); doc = getAsDOM("wms?service=WMS&request=getCapabilities&version=1.1.1", true); assertTrue(xpath.getMatchingNodes("//Exception/Format", doc).getLength() == 4); } finally { JSONType.setJsonpEnabled(jsonpOriginal); } }
/** Tests jsonp with custom callback function */ @Test public void testCustomJSONP() throws Exception { String layer = MockData.FORESTS.getPrefix() + ":" + MockData.FORESTS.getLocalPart(); String request = "wms?version=1.1.1" + "&request=DescribeLayer" + "&layers=" + layer + "&query_layers=" + layer + "&width=20&height=20" + "&outputFormat=" + JSONType.jsonp + "&format_options=" + JSONType.CALLBACK_FUNCTION_KEY + ":DescribeLayer"; JSONType.setJsonpEnabled(true); String result = getAsString(request); JSONType.setJsonpEnabled(false); checkJSONPDescribeLayer(result, layer); }
/** Constructor for subclasses */ public JSONDescribeLayerResponse(final WMS wms, final String outputFormat) { super(outputFormat); this.wms = wms; this.type = JSONType.getJSONType(outputFormat); if (type == null) throw new IllegalArgumentException("Not supported mime type for:" + outputFormat); }
JSONType.useJsonp(getMimeType(featureTypeInfos, describeFeatureType)); if (jsonp) { outWriter.write(getCallbackFunction() + "(");
/** Returns the mime type */ public String getMimeType(Object value, Operation operation) throws ServiceException { if (jsonp) { return JSONType.JSONP.getMimeType(); } else { return JSONType.JSON.getMimeType(); } }
private String getIdOption() { // include fid? String id_option = null; // null - default, "" - none, or "property" Request request = Dispatcher.REQUEST.get(); if (request != null) { id_option = JSONType.getIdPolicy(request.getKvp()); } return id_option; }
/** Encodes a ogc:ServiceExceptionReport to output. */ @Override public void handleServiceException(ServiceException exception, Request request) { boolean verbose = gs.getSettings().isVerboseExceptions(); String charset = gs.getSettings().getCharset(); setHttpHeaders(exception, request); // first of all check what kind of exception handling we must perform final String exceptions; try { exceptions = (String) request.getKvp().get("EXCEPTIONS"); if (exceptions == null) { // use default super.handleServiceException(exception, request); return; } } catch (Exception e) { super.handleServiceException(exception, request); return; } if (JSONType.isJsonMimeType(exceptions)) { // use Json format JSONType.handleJsonException(LOGGER, exception, request, charset, verbose, false); } else if (JSONType.useJsonp(exceptions)) { // use JsonP format JSONType.handleJsonException(LOGGER, exception, request, charset, verbose, true); } else { super.handleServiceException(exception, request); } }
"JSON", "wms:WMS_Capabilities/wms:Capability/wms:Exception/wms:Format[4]", doc); boolean jsonpOriginal = JSONType.isJsonpEnabled(); try { JSONType.setJsonpEnabled(true); doc = getAsDOM("wms?service=WMS&request=getCapabilities&version=1.3.0", true); assertXpathEvaluatesTo( "count(wms:WMS_Capabilities/wms:Capability/wms:Exception/wms:Format)", doc); JSONType.setJsonpEnabled(false); doc = getAsDOM("wms?service=WMS&request=getCapabilities&version=1.3.0", true); assertXpathEvaluatesTo( doc); } finally { JSONType.setJsonpEnabled(jsonpOriginal);
@Test public void testDescribePrimitiveGeoFeatureJSONP() throws Exception { JSONType.setJsonpEnabled(true); String output = getAsString( "wfs?service=WFS&request=DescribeFeatureType&version=1.0.0&outputFormat=" + JSONType.jsonp + "&typeName=" + getLayerId(SystemTestData.PRIMITIVEGEOFEATURE)); JSONType.setJsonpEnabled(false); // removing specific parts output = output.substring(0, output.length() - 2); output = output.substring(JSONType.CALLBACK_FUNCTION.length() + 1, output.length()); testOutput(output); }
@Test public void testJSONType() { // ENUM type JSONType json = JSONType.JSON; assertEquals(JSONType.JSON, json); JSONType jsonp = JSONType.JSONP; assertEquals(JSONType.JSONP, jsonp); assertEquals(JSONType.JSON, JSONType.getJSONType(JSONType.json)); assertEquals(JSONType.JSONP, JSONType.getJSONType(JSONType.jsonp)); }
if (JSONType.isJsonMimeType(exceptions)) { JSONType.handleJsonException(LOGGER, exception, request, charset, verbose, false); return; } else if (JSONType.useJsonp(exceptions)) { JSONType.handleJsonException(LOGGER, exception, request, charset, verbose, true); return; } else if (isImageExceptionType(exceptions)) {
private String getCallbackFunction() { Request request = Dispatcher.REQUEST.get(); if (request == null) { return JSONType.CALLBACK_FUNCTION; } return JSONType.getCallbackFunction(request.getKvp()); }
@Override public boolean exclude(String beanId, Object bean) { return !JSONType.isJsonpEnabled() && super.exclude(beanId, bean); } }