List of usage examples for com.google.gson JsonObject toString
@Override
public String toString()
From source file:arces.unibo.SEPA.commons.protocol.SPARQL11Protocol.java
License:Open Source License
public SPARQL11Protocol(SPARQL11Properties properties) throws IllegalArgumentException { if (properties == null) { logger.fatal("Properties are null"); throw new IllegalArgumentException("Properties are null"); }/* ww w . j a v a 2 s . co m*/ this.properties = properties; responseHandler = new ResponseHandler<String>() { @Override public String handleResponse(final HttpResponse response) { /*SPARQL 1.1 Protocol (https://www.w3.org/TR/sparql11-protocol/) UPDATE 2.2 update operation The response to an update request indicates success or failure of the request via HTTP response status code. QUERY 2.1.5 Accepted Response Formats Protocol clients should use HTTP content negotiation [RFC2616] to request response formats that the client can consume. See below for more on potential response formats. 2.1.6 Success Responses The SPARQL Protocol uses the response status codes defined in HTTP to indicate the success or failure of an operation. Consult the HTTP specification [RFC2616] for detailed definitions of each status code. While a protocol service should use a 2XX HTTP response code for a successful query, it may choose instead to use a 3XX response code as per HTTP. The response body of a successful query operation with a 2XX response is either: a SPARQL Results Document in XML, JSON, or CSV/TSV format (for SPARQL Query forms SELECT and ASK); or, an RDF graph [RDF-CONCEPTS] serialized, for example, in the RDF/XML syntax [RDF-XML], or an equivalent RDF graph serialization, for SPARQL Query forms DESCRIBE and CONSTRUCT). The content type of the response to a successful query operation must be the media type defined for the format of the response body. 2.1.7 Failure Responses The HTTP response codes applicable to an unsuccessful query operation include: 400 if the SPARQL query supplied in the request is not a legal sequence of characters in the language defined by the SPARQL grammar; or, 500 if the service fails to execute the query. SPARQL Protocol services may also return a 500 response code if they refuse to execute a query. This response does not indicate whether the server may or may not process a subsequent, identical request or requests. The response body of a failed query request is implementation defined. Implementations may use HTTP content negotiation to provide human-readable or machine-processable (or both) information about the failed query request. A protocol service may use other 4XX or 5XX HTTP response codes for other failure conditions, as per HTTP. */ JsonObject json = new JsonObject(); // Status code int code = response.getStatusLine().getStatusCode(); //Body String body = null; HttpEntity entity = response.getEntity(); try { body = EntityUtils.toString(entity, Charset.forName("UTF-8")); } catch (ParseException e) { code = 500; body = e.getMessage(); } catch (IOException e) { code = 500; body = e.getMessage(); } JsonObject jsonBody = null; try { jsonBody = new JsonParser().parse(body).getAsJsonObject(); } catch (JsonParseException | IllegalStateException e) { json.add("body", new JsonPrimitive(body)); } if (jsonBody != null) json.add("body", jsonBody); json.add("code", new JsonPrimitive(code)); return json.toString(); } }; }
From source file:arces.unibo.SEPA.server.protocol.HTTPGate.java
License:Open Source License
/** * Echo request./* w w w . j a v a2 s.c o m*/ * * @param exchange * the HTTP exchange information */ private void echoRequest(HttpExchange exchange) { JsonObject json = buildEchoResponse(exchange); if (!CORSManager.processCORSRequest(exchange)) { failureResponse(exchange, ErrorResponse.UNAUTHORIZED, "CORS origin not allowed"); return; } if (CORSManager.isPreFlightRequest(exchange)) sendResponse(exchange, 204, null); else sendResponse(exchange, 200, json.toString()); }
From source file:arces.unibo.SEPA.server.protocol.HTTPGate.java
License:Open Source License
/** * Failure response.//from w w w . jav a2 s. c o m * * @param exchange * the exchange * @param httpResponseCode * the http response code * @param responseBody * the response body */ protected void failureResponse(HttpExchange exchange, int httpResponseCode, String responseBody) { JsonObject json = buildEchoResponse(exchange); json.add("body", new JsonPrimitive(responseBody)); json.add("code", new JsonPrimitive(httpResponseCode)); sendResponse(exchange, httpResponseCode, json.toString()); }
From source file:at.gv.egovernment.moa.id.protocols.oauth20.protocol.OAuth20TokenAction.java
License:EUPL
public SLOInformationInterface processRequest(IRequest req, HttpServletRequest httpReq, HttpServletResponse httpResp, IAuthData authData) throws MOAIDException { OAuth20SessionObject auth20SessionObject = null; try {//from w w w .j ava2s . co m OAuth20TokenRequest oAuthRequest = (OAuth20TokenRequest) req; try { Logger.debug("Loaded OAuth20SessionObject from session: " + oAuthRequest.getCode()); auth20SessionObject = AssertionStorage.getInstance().get(oAuthRequest.getCode(), OAuth20SessionObject.class); } catch (MOADatabaseException e) { throw new OAuth20UnauthorizedClientException(); } // do checking for different grant types and code if (auth20SessionObject == null || !auth20SessionObject.getCode().equals(oAuthRequest.getCode())) { throw new OAuth20UnauthorizedClientException(); } else { Logger.debug("Loaded of OAuth20SessionObject was successful"); } // create response JsonObject jsonObject = new JsonObject(); OAuth20Util.addProperytiesToJsonObject(jsonObject, auth20SessionObject.getAuthDataSession()); String jsonResponse = jsonObject.toString(); Logger.debug("JSON Response: " + jsonResponse); // write respone to http response httpResp.setContentType("application/json"); httpResp.setStatus(HttpServletResponse.SC_OK); httpResp.getOutputStream().print(jsonResponse); httpResp.getOutputStream().close(); return null; } catch (Exception e) { Logger.error(e.getMessage(), e); throw new OAuth20ServerErrorException(); } finally { if (auth20SessionObject != null) { // destroy session for clean up Logger.debug("Going to destroy session: " + auth20SessionObject.getCode()); AssertionStorage.getInstance().remove(auth20SessionObject.getCode()); } } }
From source file:at.storm.bolt.JsonBolt.java
License:Apache License
public void execute(Tuple tuple, BasicOutputCollector collector) { String jsonString = tuple.getStringByField("jsonString"); TupleStatistic tupleStatistic = (TupleStatistic) tuple.getValueByField("tupleStatistic"); JsonObject jsonObject = (JsonObject) jsonParser.parse(jsonString); if (m_logging) { LOG.info("JSON: " + jsonObject.toString()); }// w ww .ja va2s. c om tupleStatistic.setRealStart(jsonObject.get("timestamp").getAsLong()); String message = jsonObject.get("msg").getAsString(); if (!LanguageDetection.isEnglish(message)) { JsonObject score = new JsonObject(); score.addProperty("score", 0); jsonObject.add("sentiment", score); collector.emit(TO_REDIS_PUBLISH_STREAM, new JsonBoltData(jsonObject, tupleStatistic)); } else { collector.emit(PIPELINE_STREAM, new JsonBoltData(jsonObject, tupleStatistic)); } }
From source file:at.storm.bolt.PreprocessorBolt.java
License:Apache License
@Override public void execute(Tuple tuple, BasicOutputCollector collector) { TokenizerBoltData tokenizerBoltValue = TokenizerBoltData.getFromTuple(tuple); JsonObject jsonObject = tokenizerBoltValue.getJsonObject(); TupleStatistic tupleStatistic = tokenizerBoltValue.getTupleStatistic(); List<String> tokens = (List<String>) tokenizerBoltValue.getTokens(); // Preprocess List<String> preprocessedTokens = m_preprocessor.preprocess(tokens); if (m_logging) { LOG.info("PREPROCESSOR: TOKENS " + preprocessedTokens + " JSON: " + jsonObject.toString()); }/*from w w w .j av a2s . com*/ // Emit new tuples collector.emit(new PreprocessorBoltData(jsonObject, tupleStatistic, preprocessedTokens)); }
From source file:at.storm.bolt.SVMBolt.java
License:Apache License
public void execute(Tuple tuple) { FeatureGenerationBoltData featureGenerationValue = FeatureGenerationBoltData.getFromTuple(tuple); JsonObject jsonObject = featureGenerationValue.getJsonObject(); TupleStatistic tupleStatistic = featureGenerationValue.getTupleStatistic(); Map<Integer, Double> featureVector = featureGenerationValue.getFeatureVector(); // Create feature nodes svm_node[] testNodes = new svm_node[featureVector.size()]; int i = 0;/* ww w . j ava 2s . c o m*/ for (Map.Entry<Integer, Double> feature : featureVector.entrySet()) { svm_node node = new svm_node(); node.index = feature.getKey(); node.value = feature.getValue(); testNodes[i] = node; i++; } double predictedClass = svm.svm_predict(m_model, testNodes); JsonObject score = new JsonObject(); score.addProperty("score", convertRating(predictedClass)); jsonObject.add("sentiment", score); if (m_logging) { LOG.info("SVM all done log: " + jsonObject.toString()); } collector.emit(tuple, new SVMBoltData(jsonObject, tupleStatistic)); collector.ack(tuple); }
From source file:at.storm.bolt.TokenizerBolt.java
License:Apache License
@Override public void execute(Tuple tuple, BasicOutputCollector collector) { // asdJsonBoltData jsonBoltValue = JsonBoltData.getFromTuple(tuple); // JsonObject jsonObject = jsonBoltValue.getJsonObject(); // TupleStatistic tupleStatistic = jsonBoltValue.getTupleStatistic(); // String msg = jsonObject.get("msg").getAsString(); RedisSpoutData redisSpoutValue = RedisSpoutData.getFromTuple(tuple); JsonObject jsonObject = redisSpoutValue.getJsonObject(); TupleStatistic tupleStatistic = redisSpoutValue.getTupleStatistic(); String msg = jsonObject.get("msg").getAsString(); List<String> tokens = Tokenizer.tokenize(msg); if (m_logging) { LOG.info("Tweet: \"" + msg + "\" Tokenized: " + tokens + " json: " + jsonObject.toString()); }//from w w w . j a v a 2 s . com // Emit new tuples collector.emit(new TokenizerBoltData(jsonObject, tupleStatistic, tokens)); }
From source file:at.tugraz.kmi.medokyservice.fca.db.DataObject.java
License:Open Source License
/** * Returns a JSON string representation of the object using the * {@link JSONMapper}//w ww. j a v a2 s . c o m * * @return a JSON string representation of the object */ @Override public String toString() { try { return JSONMapper.getInstance().toJson(this); } catch (Exception e) { JsonObject js = new JsonObject(); js.addProperty("id", id); js.addProperty("name", name); js.addProperty("description", description); return js.toString(); } }
From source file:at.tugraz.kmi.medokyservice.fca.util.ImportExport.java
License:Open Source License
private String export() { JsonObject output = new JsonObject(); output.add(SECTION_U, users2JSON()); output.add(SECTION_LO, learningObjects2JSON()); output.add(SECTION_O, objects2JSON()); output.add(SECTION_A, attributes2JSON()); output.add(SECTION_M, metadata2JSON()); output.add(SECTION_D, domains2JSON()); output.add(SECTION_C, courses2JSON()); return output.toString(); }