List of usage examples for org.apache.commons.lang3 StringEscapeUtils escapeJson
public static final String escapeJson(final String input)
Escapes the characters in a String using Json String rules.
Escapes any values it finds into their Json String form.
From source file:com.yahoo.ycsb.db.PostgreSQLJsonbClient.java
private String getJsonString(HashMap<String, ByteIterator> values) { StringBuilder builder = new StringBuilder("{"); int count = 0; for (Map.Entry<String, String> entry : StringByteIterator.getStringMap(values).entrySet()) { builder.append("\"" + entry.getKey() + "\":"); builder.append("\"" + StringEscapeUtils.escapeJson(entry.getValue()) + "\""); if (count < values.size() - 1) { builder.append(","); }/*from w ww . ja va2s . c o m*/ count++; } builder.append("}"); return builder.toString(); }
From source file:com.jkoolcloud.tnt4j.format.JSONFormatter.java
/** * Format a given {@link Snapshot} into JSON format * * @param snap// w w w. j av a 2s . com * snapshot object to be formatted into JSON * @see Snapshot */ @Override public String format(Snapshot snap) { StringBuilder jsonString = new StringBuilder(1024); jsonString.append(START_JSON); if (!Utils.isEmpty(snap.getTrackingId())) { jsonString.append(JSON_TRACK_ID_LABEL).append(ATTR_SEP).append(Utils.quote(snap.getTrackingId())) .append(ATTR_JSON); } if (!Utils.isEmpty(snap.getSignature())) { jsonString.append(JSON_TRACK_SIGN_LABEL).append(ATTR_SEP).append(Utils.quote(snap.getSignature())) .append(ATTR_JSON); } if (!Utils.isEmpty(snap.getParentId())) { jsonString.append(JSON_PARENT_TRACK_ID_LABEL).append(ATTR_SEP).append(Utils.quote(snap.getParentId())) .append(ATTR_JSON); } if (!Utils.isEmpty(snap.getId())) { String escaped = StringEscapeUtils.escapeJson(snap.getId()); // escape double quote chars jsonString.append(JSON_FQN_LABEL).append(ATTR_SEP).append(Utils.quote(escaped)).append(ATTR_JSON); } if (!Utils.isEmpty(snap.getCategory())) { jsonString.append(JSON_CATEGORY_LABEL).append(ATTR_SEP).append(Utils.quote(snap.getCategory())) .append(ATTR_JSON); } if (!Utils.isEmpty(snap.getName())) { String escaped = StringEscapeUtils.escapeJson(snap.getName()); // escape double quote chars jsonString.append(JSON_NAME_LABEL).append(ATTR_SEP).append(Utils.quote(escaped)).append(ATTR_JSON); } jsonString.append(JSON_COUNT_LABEL).append(ATTR_SEP).append(snap.size()).append(ATTR_JSON); jsonString.append(JSON_TIME_USEC_LABEL).append(ATTR_SEP).append(snap.getTimeStamp().getTimeUsec()) .append(ATTR_JSON); jsonString.append(JSON_TTL_SEC_LABEL).append(ATTR_SEP).append(snap.getTTL()).append(ATTR_JSON); Source source = snap.getSource(); if (source != null) { jsonString.append(JSON_SOURCE_LABEL).append(ATTR_SEP) .append(Utils.quote(StringEscapeUtils.escapeJson(source.getName()))).append(ATTR_JSON); String ssn = source.getSourceFactory().getSSN(); if (!Utils.isEmpty(ssn)) { String escaped = StringEscapeUtils.escapeJson(ssn); // escape double quote chars jsonString.append(JSON_SOURCE_SSN_LABEL).append(ATTR_SEP).append(Utils.quote(escaped)) .append(ATTR_JSON); } jsonString.append(JSON_SOURCE_FQN_LABEL).append(ATTR_SEP) .append(Utils.quote(StringEscapeUtils.escapeJson(source.getFQName()))).append(ATTR_JSON); if (!Utils.isEmpty(source.getUrl())) { String escaped = StringEscapeUtils.escapeJson(source.getUrl()); // escape double quote chars jsonString.append(JSON_SOURCE_URL_LABEL).append(ATTR_SEP).append(Utils.quote(escaped)) .append(ATTR_JSON); } } jsonString.append(JSON_SEVERITY_LABEL).append(ATTR_SEP).append(Utils.quote(snap.getSeverity())) .append(ATTR_JSON); jsonString.append(JSON_SEVERITY_NO_LABEL).append(ATTR_SEP).append(snap.getSeverity().ordinal()) .append(ATTR_JSON); jsonString.append(JSON_TYPE_LABEL).append(ATTR_SEP).append(Utils.quote(snap.getType())).append(ATTR_JSON); jsonString.append(JSON_TYPE_NO_LABEL).append(ATTR_SEP).append(snap.getType().ordinal()); if (snap.size() > 0) { jsonString.append(ATTR_JSON); jsonString.append(JSON_PROPERTIES_LABEL).append(ATTR_SEP).append(ARRAY_START_JSON) .append(itemsToJSON(snap.getSnapshot())).append(ARRAY_END); } jsonString.append(END_JSON); return jsonString.toString(); }
From source file:com.yahoo.ycsb.db.PostgreSQLJsonbClient.java
private String getJsonUpdateString(HashMap<String, ByteIterator> values) { String prevColumn = "fields"; for (Map.Entry<String, String> entry : StringByteIterator.getStringMap(values).entrySet()) { StringBuilder builder = new StringBuilder(); builder.append("jsonb_set(" + prevColumn + ", '{\"" + entry.getKey() + "\"}', "); builder.append("'\"" + StringEscapeUtils.escapeJson(entry.getValue()) + "\"')"); prevColumn = builder.toString(); }/*from w w w .j av a2 s . c o m*/ return prevColumn; }
From source file:com.jkoolcloud.tnt4j.format.JSONFormatter.java
/** * Format a given {@link Property} into JSON format * * @param prop property object to be formatted into JSON * @return formatted property as a JSON string * @see Property//from w w w .j av a 2 s . com */ public String format(Property prop) { StringBuilder jsonString = new StringBuilder(1024); jsonString.append(START_JSON); Object value = prop.getValue(); jsonString.append(JSON_NAME_LABEL).append(ATTR_SEP) .append(Utils.quote(StringEscapeUtils.escapeJson(prop.getKey()))).append(ATTR_JSON); jsonString.append(JSON_TYPE_LABEL).append(ATTR_SEP).append(Utils.quote(prop.getDataType())) .append(ATTR_JSON); if (prop.getValueType() != null && !prop.getValueType().equalsIgnoreCase(ValueTypes.VALUE_TYPE_NONE)) { jsonString.append(JSON_VALUE_TYPE_LABEL).append(ATTR_SEP).append(Utils.quote(prop.getValueType())) .append(ATTR_JSON); } if (value instanceof Number) { jsonString.append(JSON_VALUE_LABEL).append(ATTR_SEP).append(value); } else { String valueText = StringEscapeUtils.escapeJson(String.valueOf(value)); jsonString.append(JSON_VALUE_LABEL).append(ATTR_SEP).append(Utils.quote(valueText)); } jsonString.append(END_JSON); return jsonString.toString(); }
From source file:com.jkoolcloud.tnt4j.format.JSONFormatter.java
@Override public String format(long ttl, Source source, OpLevel level, String msg, Object... args) { StringBuilder jsonString = new StringBuilder(1024); jsonString.append(START_JSON);//w w w. ja va 2 s .co m jsonString.append(JSON_SEVERITY_LABEL).append(ATTR_SEP).append(Utils.quote(level)).append(ATTR_JSON); jsonString.append(JSON_SEVERITY_NO_LABEL).append(ATTR_SEP).append(level.ordinal()).append(ATTR_JSON); jsonString.append(JSON_TYPE_LABEL).append(ATTR_SEP).append(Utils.quote(OpType.DATAGRAM)).append(ATTR_JSON); jsonString.append(JSON_TYPE_NO_LABEL).append(ATTR_SEP).append(OpType.DATAGRAM.ordinal()).append(ATTR_JSON); jsonString.append(JSON_PID_LABEL).append(ATTR_SEP).append(Utils.getVMPID()).append(ATTR_JSON); jsonString.append(JSON_TID_LABEL).append(ATTR_SEP).append(Thread.currentThread().getId()).append(ATTR_JSON); String usrName = StringEscapeUtils.escapeJson( source == null ? DefaultSourceFactory.getInstance().getRootSource().getUser() : source.getUser()); jsonString.append(JSON_USER_LABEL).append(ATTR_SEP).append(Utils.quote(usrName)).append(ATTR_JSON); jsonString.append(JSON_TTL_SEC_LABEL).append(ATTR_SEP).append(ttl).append(ATTR_JSON); jsonString.append(JSON_TIME_USEC_LABEL).append(ATTR_SEP).append(Useconds.CURRENT.get()).append(ATTR_JSON); jsonString.append(JSON_OPERATION_LABEL).append(ATTR_SEP).append(Utils.quote(defOpName)).append(ATTR_JSON); if (source != null) { jsonString.append(JSON_SOURCE_LABEL).append(ATTR_SEP) .append(Utils.quote(StringEscapeUtils.escapeJson(source.getName()))).append(ATTR_JSON); String ssn = source.getSourceFactory().getSSN(); if (!Utils.isEmpty(ssn)) { String escaped = StringEscapeUtils.escapeJson(ssn); // escape double quote chars jsonString.append(JSON_SOURCE_SSN_LABEL).append(ATTR_SEP).append(Utils.quote(escaped)) .append(ATTR_JSON); } jsonString.append(JSON_SOURCE_FQN_LABEL).append(ATTR_SEP) .append(Utils.quote(StringEscapeUtils.escapeJson(source.getFQName()))); if (!Utils.isEmpty(source.getUrl())) { jsonString.append(ATTR_JSON); String escaped = StringEscapeUtils.escapeJson(source.getUrl()); // escape double quote chars jsonString.append(JSON_SOURCE_URL_LABEL).append(ATTR_SEP).append(Utils.quote(escaped)); } Source geoloc = source.getSource(SourceType.GEOADDR); if (geoloc != null) { jsonString.append(ATTR_JSON); jsonString.append(JSON_LOCATION_LABEL).append(ATTR_SEP).append(Utils.quote(geoloc.getName())); } } if (!Utils.isEmpty(msg)) { String msgText = Utils.format(msg, args); msgText = StringEscapeUtils.escapeJson(msgText); // escape double quote chars jsonString.append(ATTR_JSON); jsonString.append(JSON_MSG_TEXT_LABEL).append(ATTR_SEP).append(Utils.quote(msgText)); } Throwable ex = Utils.getThrowable(args); if (ex != null) { jsonString.append(ATTR_JSON); String excText = StringEscapeUtils.escapeJson(ex.toString()); // escape double quote chars jsonString.append(JSON_EXCEPTION_LABEL).append(ATTR_SEP).append(Utils.quote(excText)); } jsonString.append(END_JSON); return jsonString.toString(); }
From source file:com.jkoolcloud.tnt4j.format.JSONFormatter.java
private String itemsToJSON(Collection<?> items) { if (items == null) return ""; StringBuilder json = new StringBuilder(2048); for (Object item : items) { if (json.length() > 0) json.append(ATTR_JSON);//from w w w. j a v a 2 s . c o m if (item instanceof TrackingEvent) { json.append(format((TrackingEvent) item)); } else if (item instanceof TrackingActivity) { json.append(format((TrackingActivity) item)); } else if (item instanceof Snapshot) { json.append(format((Snapshot) item)); } else if (item instanceof Property) { json.append(format((Property) item)); } else { String vText = StringEscapeUtils.escapeJson(String.valueOf(item)); // escape double quote chars json.append(Utils.quote(vText)); } } return json.toString(); }
From source file:com.sonicle.webtop.core.app.WebTopSession.java
private void fillServiceManifest(JsWTS js, ServiceManifest manifest, Locale locale, boolean maintenance) { JsWTS.Manifest jsman = js.createManifestInstance(); jsman.xid = manifest.getXId();//ww w.j av a2s.co m jsman.ns = manifest.getJsPackageName(); jsman.path = manifest.getJsBaseUrl(false); jsman.name = StringEscapeUtils .escapeJson(wta.lookupResource(manifest.getId(), locale, CoreLocaleKey.SERVICE_NAME)); jsman.description = StringEscapeUtils .escapeJson(wta.lookupResource(manifest.getId(), locale, CoreLocaleKey.SERVICE_DESCRIPTION)); jsman.company = StringEscapeUtils.escapeJson(manifest.getCompany()); jsman.localeCN = manifest.getLocaleJsClassName(locale, true); jsman.maintenance = maintenance; if (jsman instanceof JsWTSPrivate.PrivateManifest) { ((JsWTSPrivate.PrivateManifest) jsman).version = manifest.getVersion().toString(); ((JsWTSPrivate.PrivateManifest) jsman).build = manifest.getBuildDate(); } js.manifests.put(manifest.getId(), jsman); //JsWTS.Manifest jsman = new JsWTS.Manifest(); //jsman.xid = manifest.getXId(); //jsman.ns = manifest.getJsPackageName(); //jsman.path = manifest.getJsBaseUrl(false); //jsman.name = StringEscapeUtils.escapeJson(wta.lookupResource(manifest.getId(), locale, CoreLocaleKey.SERVICE_NAME)); //jsman.description = StringEscapeUtils.escapeJson(wta.lookupResource(manifest.getId(), locale, CoreLocaleKey.SERVICE_DESCRIPTION)); //jsman.version = manifest.getVersion().toString(); //jsman.build = manifest.getBuildDate(); //jsman.company = StringEscapeUtils.escapeJson(manifest.getCompany()); //jsman.localeClassName = manifest.getLocaleJsClassName(locale, true); //js.manifests.put(manifest.getId(), jsman); //String localizedName = wta.lookupResource(manifest.getId(), locale, CoreLocaleKey.SERVICE_NAME); //String localizedDescription = wta.lookupResource(manifest.getId(), locale, CoreLocaleKey.SERVICE_DESCRIPTION); //js.manifests.put(manifest.getId(), new JsWTS.Manifest(manifest, localizedName, localizedDescription, manifest.getLocaleJsClassName(locale, true))); }
From source file:com.crushpaper.Servlet.java
/** Part of the JSON API. Returns the matches as JSON. */ private void handleJsonSearchNotes(RequestAndResponse requestAndResponse) throws IOException, ServletException { requestAndResponse.setResponseContentTypeJson(); String query = requestAndResponse.getParameter("q"); if (query == null) { query = ""; }/* w ww . jav a2 s .c o m*/ query = query.trim(); if (!isUserSignedIn(requestAndResponse)) { returnJson400(requestAndResponse, servletText.errorRequiresSignIn(allowSaveIfNotSignedIn)); } else if (isUsersAccountClosed(requestAndResponse)) { returnJson400(requestAndResponse, servletText.errorAccountIsClosed()); } else { try { final StringBuilder result = new StringBuilder(); final User user = dbLogic.getUserById(getEffectiveUserId(requestAndResponse)); if (user != null) { final ResultsPaginator paginator = new ResultsPaginator(requestAndResponse, null, result, null); result.append("{ \"results\": ["); if (user != null) { boolean first = true; final List<?> results = dbLogic.searchEntriesForUserByNote(user.getId(), query, paginator.getStartPosition(), paginator.getMaxResults()); for (final Object entryUncasted : results) { final Entry entry = (Entry) entryUncasted; final int resultNumber = paginator.next(); if (resultNumber == -1) { continue; } else if (resultNumber == 0) { break; } if (!first) { result.append(","); first = false; } result.append("\n"); result.append("{ \"id\":\""); result.append(entry.getId()); result.append("\", \"note\":\""); result.append(StringEscapeUtils.escapeJson(entry.getNoteOrTitle(""))); result.append("\", \"quotation\":\""); result.append(StringEscapeUtils.escapeJson(entry.getQuotation(""))); result.append("\"}"); } } result.append("\n],\n\"more\": " + (paginator.hasMore() ? "true" : "false") + " }\n"); } dbLogic.commit(); requestAndResponse.print(result.toString()); } catch (EmptyQueryException e) { returnJson400(requestAndResponse, servletText.errorNeedLongerQuery()); } catch (final PersistenceException e) { logger.log(Level.INFO, "Exception", e); returnJson500(requestAndResponse, servletText.errorInternalDatabase()); } } }
From source file:net.thewaffleshop.passwd.security.AuthenticationHandler.java
/** * Successful login; send the redirect URL * * @param request//from ww w .j a v a 2 s. co m * @param response * @param authentication * @throws IOException * @throws ServletException */ @Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { AccountAuthenticationToken auth = (AccountAuthenticationToken) authentication; HttpSession session = request.getSession(); session.setAttribute("account", auth.getAccount()); session.setAttribute("secretKey", auth.getSecretKey()); String url = ServletUriComponentsBuilder.fromContextPath(request).path(TARGET_URL).build().toUriString(); response.setStatus(HttpServletResponse.SC_OK); response.setContentType("application/json"); response.getWriter().write("{\"success\": true, \"url\": \"" + StringEscapeUtils.escapeJson(url) + "\"}"); }
From source file:net.thewaffleshop.passwd.security.AuthenticationHandler.java
/** * Unsuccessful login; send error// ww w .j av a2 s.c om * * @param request * @param response * @param exception * @throws IOException * @throws ServletException */ @Override public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { String msg = exception.getMessage(); response.setStatus(HttpServletResponse.SC_OK); response.setContentType("application/json"); response.getWriter().write("{\"success\": false, \"msg\": \"" + StringEscapeUtils.escapeJson(msg) + "\"}"); }