use of com.google.gson.JsonParseException in project pulsar by yahoo.
the class DiscoveryServiceWebTest method hitBrokerService.
public String hitBrokerService(String method, String url, Object data) throws JsonParseException {
Response response = null;
try {
WebTarget webTarget = client.target(url);
Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
if (HttpMethod.PUT.equals(method)) {
response = (Response) invocationBuilder.put(Entity.entity(data, MediaType.APPLICATION_JSON));
} else if (HttpMethod.GET.equals(method)) {
response = (Response) invocationBuilder.get();
} else if (HttpMethod.POST.equals(method)) {
response = (Response) invocationBuilder.post(Entity.entity(data, MediaType.APPLICATION_JSON));
} else {
fail("Unsupported http method");
}
} catch (Exception e) {
// fail
fail();
}
JsonObject jsonObject = new Gson().fromJson(response.readEntity(String.class), JsonObject.class);
String serviceResponse = jsonObject.get("reason").getAsString();
return serviceResponse;
}
use of com.google.gson.JsonParseException in project SEPA by arces-wot.
the class Context method loadUsers.
private void loadUsers() {
FileReader in = null;
try {
in = new FileReader("usersDB.json");
} catch (FileNotFoundException e) {
return;
}
if (in != null) {
try {
db = new JsonParser().parse(in).getAsJsonObject();
} catch (JsonParseException | IllegalStateException e) {
return;
}
for (Entry<String, JsonElement> record : db.entrySet()) {
String id = record.getKey();
String user = record.getValue().getAsJsonObject().get("user").getAsString();
Boolean authorized = record.getValue().getAsJsonObject().get("authorized").getAsBoolean();
users.put(id, user);
authorizations.put(id, authorized);
}
}
}
use of com.google.gson.JsonParseException in project SEPA by arces-wot.
the class SPARQL11Protocol method update.
/**
* Implements a SPARQL 1.1 update operation
* (https://www.w3.org/TR/sparql11-protocol/)
*
* <pre>
* update via URL-encoded POST
* - HTTP Method: POST
* - Query String Parameters: None
* - Request Content Type: <b>application/x-www-form-urlencoded</b>
* - Request Message Body: URL-encoded, ampersand-separated query parameters. <b>update</b> (exactly 1). using-graph-uri (0 or more). using-named-graph-uri (0 or more)
*
* update via POST directly
* - HTTP Method: POST
* - Query String parameters: using-graph-uri (0 or more); using-named-graph-uri (0 or more)
* - Request Content Type: <b>application/sparql-update</b>
* - Request Message Body: Unencoded SPARQL update request string
* </pre>
*
* UPDATE 2.2 update operation The response to an update request indicates
* success or failure of the request via HTTP response status code.
*/
public Response update(UpdateRequest req, int timeout) {
StringEntity requestEntity = null;
CloseableHttpResponse httpResponse = null;
HttpEntity responseEntity = null;
int responseCode = 0;
String responseBody = null;
try {
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout).build();
// Set request entity
if (properties.getQueryMethod().equals(HTTPMethod.GET)) {
// ***********************
// OpenLink VIRTUOSO PATCH
// ***********************
// SPARQL 1.1 Update are issued as GET requests using the "query" URL parameter
// The "default-graph-uri" parameter is REQUIRED
String query = "query=" + URLEncoder.encode(req.getSPARQL(), "UTF-8") + "&format=" + URLEncoder.encode(properties.getUpdateAcceptHeader(), "UTF-8");
if (properties.getDefaultGraphURI() != null) {
query += "&default-graph-uri=" + URLEncoder.encode(properties.getDefaultGraphURI(), "UTF-8");
}
String url;
if (properties.getHttpPort() != -1)
url = "http://" + properties.getHost() + ":" + properties.getHttpPort() + "/" + properties.getUpdatePath() + "?" + query;
else
url = "http://" + properties.getHost() + "/" + properties.getUpdatePath() + "?" + query;
HttpGet queryGetRequest;
queryGetRequest = new HttpGet(url);
queryGetRequest.setConfig(requestConfig);
updateRequest = queryGetRequest;
} else {
if (properties.getUpdateMethod().equals(HTTPMethod.URL_ENCODED_POST)) {
requestEntity = new StringEntity("update=" + URLEncoder.encode(req.getSPARQL(), "UTF-8"));
} else if (properties.getUpdateMethod().equals(HTTPMethod.POST)) {
requestEntity = new StringEntity(req.getSPARQL(), Consts.UTF_8);
}
updatePostRequest.setEntity(requestEntity);
updatePostRequest.setConfig(requestConfig);
updateRequest = updatePostRequest;
}
// Execute HTTP request
logger.debug("Execute SPARQL 1.1 QUERY (timeout: " + timeout + " ms) " + updatePostRequest.toString(), timeout);
long timing = System.nanoTime();
httpResponse = httpClient.execute(updateRequest);
timing = System.nanoTime() - timing;
logger.debug("UPDATE_TIME (" + timing / 1000000 + " ms)");
// Status code
responseCode = httpResponse.getStatusLine().getStatusCode();
// Body
responseEntity = httpResponse.getEntity();
responseBody = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
EntityUtils.consume(responseEntity);
} catch (IOException e) {
return new ErrorResponse(req.getToken(), HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
} finally {
try {
if (httpResponse != null)
httpResponse.close();
} catch (IOException e) {
return new ErrorResponse(req.getToken(), HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
}
requestEntity = null;
responseEntity = null;
}
if (responseCode >= 400) {
try {
return new ErrorResponse(req.getToken(), new JsonParser().parse(responseBody).getAsJsonObject());
} catch (JsonParseException e) {
return new ErrorResponse(req.getToken(), responseCode, responseBody);
}
}
return new UpdateResponse(req.getToken(), responseBody);
}
use of com.google.gson.JsonParseException in project gocd by gocd.
the class ConfigurationPropertyRepresenter method fromJSON.
public static ConfigurationProperty fromJSON(JsonReader jsonReader) {
try {
String key = jsonReader.getString("key");
String value = jsonReader.optString("value").orElse(null);
String encryptedValue = jsonReader.optString("encrypted_value").orElse(null);
return ConfigurationProperty.deserialize(key, value, encryptedValue);
} catch (Exception e) {
throw new JsonParseException("Could not parse configuration property");
}
}
use of com.google.gson.JsonParseException in project gocd by gocd.
the class RoleRepresenter method fromJSON.
// private static void addLinks(Role model, JsonWriter jsonWriter) {
// jsonWriter.addDocLink(Routes.Roles.DOC);
// jsonWriter.self(Routes.Roles.name(model.getName().toString()));
// jsonWriter.find(Routes.Roles.find());
// }
// public static Map toJSON(Role role, RequestContext requestContext) {
// if (role == null) return null;
//
// JsonWriter jsonWriter = new JsonWriter(requestContext);
//
// addLinks(role, jsonWriter);
//
// jsonWriter.add("name", role.getName().toString());
// jsonWriter.add("type", getRoleType(role));
// if (role.hasErrors()) {
// jsonWriter.add("errors", new ErrorGetter(Collections.singletonMap("authConfigId", "auth_config_id"))
// .apply(role, requestContext));
// }
// if (role instanceof RoleConfig) {
// jsonWriter.add("attributes", GoCDRoleConfigRepresenter.toJSON((RoleConfig) role, requestContext));
// } else if (role instanceof PluginRoleConfig) {
// jsonWriter.add("attributes", PluginRoleConfigRepresenter.toJSON((PluginRoleConfig) role, requestContext));
// }
// return jsonWriter.getAsMap();
// }
public static Role fromJSON(JsonReader jsonReader) {
Role model;
String type = jsonReader.optString("type").orElse("");
if ("gocd".equals(type)) {
model = GoCDRoleConfigRepresenter.fromJSON(jsonReader.readJsonObject("attributes"));
} else if ("plugin".equals(type)) {
model = PluginRoleConfigRepresenter.fromJSON(jsonReader.readJsonObject("attributes"));
} else {
throw new JsonParseException("Invalid role type '%s'. It has to be one of 'gocd' or 'plugin'");
}
model.setName(new CaseInsensitiveString(jsonReader.optString("name").orElse(null)));
return model;
}
Aggregations