List of usage examples for org.apache.commons.lang3 StringEscapeUtils unescapeXml
public static final String unescapeXml(final String input)
Unescapes a string containing XML entity escapes to a string containing the actual Unicode characters corresponding to the escapes.
Supports only the five basic XML entities (gt, lt, quot, amp, apos).
From source file:record.Record.java
@GET @Produces("application/xml") public Response getXml(@PathParam("uuid") String uuid) { SnannySostServerConfig snannySostServerConfig = SnannySostServerConfig.singleton(servletContext); ResponseBuilder response = null;/*w ww . jav a 2 s . c o m*/ if (snannySostServerConfig.isCouchbase()) { JsonDocument jsonDocument = CouchbaseManager.getSystemBucket().get(uuid); if (jsonDocument == null) { jsonDocument = CouchbaseManager.getObservationBucket().get(uuid); } if (jsonDocument == null) { throw new SnannySostServerException(SnannySostServerMessages.ERROR_NO_UUID_RECORD_1of2 + uuid + SnannySostServerMessages.ERROR_NO_UUID_RECORD_2of2, Status.NOT_FOUND); } try { if (snannySostServerConfig.isJsonObject()) { JSONObject jSONObject = new JSONObject( StringEscapeUtils.unescapeXml(new JsonTranscoder().jsonObjectToString( jsonDocument.content().getObject(SnannySostServerConfig.FilejsonField)))); response = Response.ok((Object) JSONML.toString(jSONObject)); } else { JSONArray jsonArray = new JSONArray( StringEscapeUtils.unescapeXml(new JsonArrayTranscoder().jsonArrayToString( jsonDocument.content().getArray(SnannySostServerConfig.FilejsonField)))); response = Response.ok((Object) JSONML.toString(jsonArray)); } } catch (Exception ex) { throw new SnannySostServerException( SnannySostServerMessages.ERROR_COUCHBASE_ERROR + ex.getMessage(), Status.SERVICE_UNAVAILABLE); } } else { File uuidFile; try { uuidFile = SnannySostServerConfig.singleton(servletContext).getSensorMlFile(uuid); } catch (SnannySostServerException ssse) { uuidFile = SnannySostServerConfig.singleton(servletContext).getOemFile(uuid); } response = Response.ok((Object) uuidFile); } return response.build(); }
From source file:sos.insert.InsertSensor.java
/** Check format, convert from JSON to XML as needed, insert sensor in sos server * //from ww w.java 2 s. c o m * @param snannySostServerConfig the sos server configuration * @param contentPost the content post with sensor in JSON or XML format * @param response Http Servlet response * @param out PrintWriter for Http Servlet Response * @param sampleBean * @throws SnannySostServerException if insertion failed */ public synchronized void insert(SnannySostServerConfig snannySostServerConfig, String contentPost, HttpServletResponse response, PrintWriter out) throws SnannySostServerException { String uuid = ""; insertSensor.reset(); // parse post content try { InputSource is = new InputSource(new StringReader(contentPost)); postParser.parse(is, insertSensor); } catch (SAXException ex) { throw new SnannySostServerException(SnannySostServerMessages.ERROR_PARSE_INSERT, Status.BAD_REQUEST); } catch (IOException ex) { throw new SnannySostServerException(SnannySostServerMessages.ERROR_IO_INSERT, Status.SERVICE_UNAVAILABLE); } // if json content convert it to xml if (format.isEmpty()) { throw new SnannySostServerException(SnannySostServerMessages.ERROR_POST_FORMAT_REQUIRED + SosFormat.getAvailableSosResponseFormats(), Status.BAD_REQUEST); } SOSFORMAT sosformat = SosFormat.getFormat(format); try { switch (sosformat) { case JSON: if (snannySostServerConfig.isJsonObject()) { JSONObject jSONOject = new JSONObject(StringEscapeUtils.unescapeXml(sensorML)); sensorML = JSONML.toString(jSONOject); } else { JSONArray jsonArray = new JSONArray(StringEscapeUtils.unescapeXml(sensorML)); sensorML = JSONML.toString(jsonArray); } break; case XML: break; } } catch (Exception ex) { throw new SnannySostServerException(SnannySostServerMessages.ERROR_JSON_INSERT + ex.getMessage(), Status.BAD_REQUEST); } // validate xml SosValidation.singleton(snannySostServerConfig).xsdValidateSensorMl(sensorML); SosValidation.singleton(snannySostServerConfig).schematronValidateSensorMl(sensorML); uuid = SensorMlUuid.singleton().getUuid(sensorML); if (snannySostServerConfig.isCouchbase()) { try { JsonObject jsonObject = JsonObject.empty(); if (snannySostServerConfig.isJsonObject()) { jsonObject.put(SnannySostServerConfig.FilejsonField, jsonTranscoder.stringToJsonObject(JSONML.toJSONObject(sensorML).toString())); } else { jsonObject.put(SnannySostServerConfig.FilejsonField, jsonArrayTranscoder.stringToJsonArray(JSONML.toJSONArray(sensorML).toString())); } jsonObject.put(SnannySostServerConfig.AuthorNameField, SnannySostServerConfig.AuthorNameValue); CouchbaseManager.getSystemBucket().upsert(JsonDocument.create(uuid, jsonObject), snannySostServerConfig.getCouchbaseTimeOutMS(), TimeUnit.MILLISECONDS); Sos.SAMPLE_SYSTEM_UUID = uuid; } catch (Exception ex) { throw new SnannySostServerException( SnannySostServerMessages.ERROR_COUCHBASE_ERROR + ex.getMessage(), Status.SERVICE_UNAVAILABLE); } } else { // write xml try { OutputStreamWriter outputStreamWriter = new OutputStreamWriter( new FileOutputStream(snannySostServerConfig.newSensorMlFile(uuid)), snannySostServerConfig.getCharset()); outputStreamWriter.write(sensorML); outputStreamWriter.close(); } catch (IOException ex) { throw new SnannySostServerException(SnannySostServerMessages.ERROR_STORE_INSERT, Status.SERVICE_UNAVAILABLE); } } Success.submit(SnannySostServerMessages.IMPORT_SENSOR_OK_1of2 + uuid + SnannySostServerMessages.IMPORT_SENSOR_OK_2of2, response, out, snannySostServerConfig); }
From source file:sos.Sos.java
/**provides sensor metadata in SensorML. The sensor description can contain information about the sensor in general, * the identifier and classification,position and observed phenomena, but also details such as calibration data. * /* w ww . ja v a 2s.c om*/ * @param request Http Servlet Request * @param response Http Servlet Response * @param out PrintWriter for Http Servlet Response * @param snannySostServerConfig the sos server configuration * @throws SnannySostServerException (I/O or conversion problem) */ private void describeSensor(HttpServletRequest request, HttpServletResponse response, PrintWriter out, SnannySostServerConfig snannySostServerConfig) throws SnannySostServerException { String uuid = getSosProcedureUuid(request); SOSFORMAT sosresponseformat = getResponseFormatParameter(request); if (snannySostServerConfig.isCouchbase()) { try { JsonDocument jsonDocument = CouchbaseManager.getSystemBucket().get(uuid); if (jsonDocument == null) { throw new SnannySostServerException(SnannySostServerMessages.ERROR_NO_UUID_RECORD_1of2 + uuid + SnannySostServerMessages.ERROR_NO_UUID_RECORD_2of2, Status.NOT_FOUND); } response.setContentType(SosFormat.getContentType(sosresponseformat)); switch (sosresponseformat) { case JSON: if (snannySostServerConfig.isJsonObject()) { out.append(new JsonTranscoder().jsonObjectToString( jsonDocument.content().getObject(SnannySostServerConfig.FilejsonField))); } else { out.append(new JsonArrayTranscoder().jsonArrayToString( jsonDocument.content().getArray(SnannySostServerConfig.FilejsonField))); } break; case XML: if (snannySostServerConfig.isJsonObject()) { JSONObject jSONObject = new JSONObject( StringEscapeUtils.unescapeXml(new JsonTranscoder().jsonObjectToString( jsonDocument.content().getObject(SnannySostServerConfig.FilejsonField)))); out.append(JSONML.toString(jSONObject)); } else { JSONArray jsonArray = new JSONArray( StringEscapeUtils.unescapeXml(new JsonArrayTranscoder().jsonArrayToString( jsonDocument.content().getArray(SnannySostServerConfig.FilejsonField)))); out.append(JSONML.toString(jsonArray)); } break; } } catch (Exception ex) { throw new SnannySostServerException( SnannySostServerMessages.ERROR_COUCHBASE_ERROR + ex.getMessage(), Status.SERVICE_UNAVAILABLE); } } else { File uuidFile = snannySostServerConfig.getSensorMlFile(uuid); try { byte[] encoded = Files.readAllBytes(Paths.get(uuidFile.toURI())); String uuidFileContent = new String(encoded, snannySostServerConfig.getCharset()); response.setContentType(SosFormat.getContentType(sosresponseformat)); switch (sosresponseformat) { case JSON: if (snannySostServerConfig.isJsonObject()) { JSONObject jSONObject = JSONML.toJSONObject(uuidFileContent); out.append(jSONObject.toString()); } else { JSONArray jsonArray = JSONML.toJSONArray(uuidFileContent); out.append(jsonArray.toString()); } break; case XML: out.append(uuidFileContent); break; } } catch (IOException io) { throw new SnannySostServerException(SnannySostServerMessages.ERROR_describeSensor, Status.SERVICE_UNAVAILABLE); } catch (JSONException je) { throw new SnannySostServerException( SnannySostServerMessages.ERROR_XML_JSON_CONVERSION + je.getMessage(), Status.SERVICE_UNAVAILABLE); } } }
From source file:sos.Sos.java
/**allows to query a specific observation using an identifier returned by the service as response to an InsertObservation operation. * /* w w w .jav a 2 s . c o m*/ * @param request Http Servlet Request * @param response Http Servlet Response * @param out PrintWriter for Http Servlet Response * @param snannySostServerConfig the sos server configuration * @throws SnannySostServerException */ private void getObservationById(HttpServletRequest request, HttpServletResponse response, PrintWriter out, SnannySostServerConfig snannySostServerConfig) throws SnannySostServerException { String uuid = getSosObservationUuid(request); SOSFORMAT sosresponseformat = getResponseFormatParameter(request); if (snannySostServerConfig.isCouchbase()) { try { JsonDocument jsonDocument = CouchbaseManager.getObservationBucket().get(uuid); if (jsonDocument == null) { throw new SnannySostServerException(SnannySostServerMessages.ERROR_NO_UUID_RECORD_1of2 + uuid + SnannySostServerMessages.ERROR_NO_UUID_RECORD_2of2, Status.NOT_FOUND); } response.setContentType(SosFormat.getContentType(sosresponseformat)); switch (sosresponseformat) { case JSON: if (snannySostServerConfig.isJsonObject()) { out.append(new JsonTranscoder().jsonObjectToString( jsonDocument.content().getObject(SnannySostServerConfig.FilejsonField))); } else { out.append(new JsonArrayTranscoder().jsonArrayToString( jsonDocument.content().getArray(SnannySostServerConfig.FilejsonField))); } break; case XML: if (snannySostServerConfig.isJsonObject()) { JSONObject jSONObject = new JSONObject( StringEscapeUtils.unescapeXml(new JsonTranscoder().jsonObjectToString( jsonDocument.content().getObject(SnannySostServerConfig.FilejsonField)))); out.append(JSONML.toString(jSONObject)); } else { JSONArray jsonArray = new JSONArray( StringEscapeUtils.unescapeXml(new JsonArrayTranscoder().jsonArrayToString( jsonDocument.content().getArray(SnannySostServerConfig.FilejsonField)))); out.append(JSONML.toString(jsonArray)); } break; } } catch (Exception ex) { throw new SnannySostServerException( SnannySostServerMessages.ERROR_COUCHBASE_ERROR + ex.getMessage(), Status.SERVICE_UNAVAILABLE); } } else { File uuidFile = SnannySostServerConfig.singleton(request.getServletContext()).getOemFile(uuid); try { byte[] encoded = Files.readAllBytes(Paths.get(uuidFile.toURI())); String uuidFileContent = new String(encoded, snannySostServerConfig.getCharset()); response.setContentType(SosFormat.getContentType(sosresponseformat)); switch (sosresponseformat) { case JSON: if (snannySostServerConfig.isJsonObject()) { JSONObject jsonObject = JSONML.toJSONObject(uuidFileContent); out.append(jsonObject.toString()); } else { JSONArray jsonArray = JSONML.toJSONArray(uuidFileContent); out.append(jsonArray.toString()); } break; case XML: out.append(uuidFileContent); break; } } catch (Exception ex) { throw new SnannySostServerException(SnannySostServerMessages.ERROR_describeSensor, Status.SERVICE_UNAVAILABLE); } } }
From source file:tds.ContentUploader.Web.backing.ContentPublishBacking.java
/** * When someone clicks on the publish HTML button then this function is called. *//* w w w .j av a 2s . com*/ public void btnPublish_OnClick() { StringBuilder rw = null; try { rw = getResponseappendr(); // ProcessXmlFiles(extractedContentPath); // get the items/stimuli from the item bank HashMap<String, ItemBankFile> items = new HashMap<String, ItemBankFile>(); HashMap<String, ItemBankFile> stimuli = new HashMap<String, ItemBankFile>(); IItemBankRepository itemPreviewRepository = _itemBankManager.CreateItemPreviewRepository(); rw.append("<p>Reading item bank... </p>"); //rw.flush (); itemPreviewRepository.getContentFiles(items, stimuli); // .GetContentFiles(out // items, out // stimuli); rw.append("<p>Extracting zip... </p>"); //rw.flush (); String extractedContentPath = extractContent(); rw.append("<p>Uploading items... </p>"); //rw.flush (); loadAndProcessFiles(items, "Item", "item", extractedContentPath); rw.append("<p>Uploading passages... </p>"); //rw.flush (); loadAndProcessFiles(stimuli, "Passage", "stim", extractedContentPath); // rw.flush (); } catch (Exception e) { _logger.error("Error during action : btnPublish_OnClick ", e); writeMessage("Error while publishing zip file : " + e.getMessage(), MessageType.ERROR); if (rw != null) { rw.append("Error: " + e.getMessage()); rw.append("<br/>"); } } finally { findComponent("contentPublishResponse").setRendered(true); setHtmlResponse(StringEscapeUtils.unescapeXml(dynamicResponse.toString())); dynamicResponse = new StringBuilder(); findComponent("pnlActions").setRendered(false); findComponent("pnlReview").setRendered(true); } }
From source file:uta.ak.usttmp.dmcore.systeminterface.DmcoreInterfaceImpl.java
@Override public Message response(String msgstr) throws Exception { Message callMsg = (Message) UsttmpXmlUtil.xmlStrToObject(StringEscapeUtils.unescapeXml(msgstr), Message.class); String msgMethod = callMsg.getMethodName(); String msgBody = callMsg.getMethodBody(); Message responseMsg = new Message(); responseMsg.setSource(UsttmpConst.SUBSYSTEM_NAME_DMCORE); responseMsg.setTarget(UsttmpConst.SUBSYSTEM_NAME_CONSOLE); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dt = formatter.format(new Date()); responseMsg.setTimeStamp(dt);// w w w .j a va 2 s. co m responseMsg.setType(UsttmpInterfaceManager.TYPE_RESPONSE); CallResult cr = new CallResult(); try { switch (msgMethod) { case "AddMiningTask": miningTaskService .addMiningTask((MiningTask) UsttmpXmlUtil.xmlStrToObject(msgBody, MiningTask.class)); break; case "StopMiningTask": miningTaskService.stopMiningTask(Long.parseLong(msgBody)); break; case "DeleteMiningTask": miningTaskService.deleteMiningTask(Long.parseLong(msgBody)); break; case "CheckStatus": break; default: throw new IllegalArgumentException("No such method"); } cr.setInfo("Operation done successfully."); cr.setResultStatus(CallResult.RESULT_SUCCESS); } catch (Exception e) { e.printStackTrace(); StringWriter errors = new StringWriter(); e.printStackTrace(new PrintWriter(errors)); cr.setError(errors.toString()); cr.setResultStatus(CallResult.RESULT_FAILED); } finally { responseMsg.setMethodBody(UsttmpXmlUtil.objectToXmlStr(cr, cr.getClass(), true)); return responseMsg; } }