@Override public Representation toRepresentation(Object source, Variant target, Resource resource) { Representation result = null; if (source instanceof JSONArray) { result = new JsonRepresentation((JSONArray) source); } else if (source instanceof JSONObject) { result = new JsonRepresentation((JSONObject) source); } else if (source instanceof JSONTokener) { result = new JsonRepresentation((JSONTokener) source); } return result; }
@Get public JSONObject getIp(JsonRepresentation j) throws JSONException { JSONObject request = j.getJsonObject(); . . . }
@Put("json") public String update(JsonRepresentation foos) { JSONArray fooArray = foos.getJsonArray(); // ... }
@Post("json") public Representation translate(JsonRepresentation jsonRepresentation) throws ResourceException { try { // No alternate versions supported. But if we did: String version = (String) getRequest().getAttributes().get("version"); if ( version == null ) { version = LayerConfiguration.DEFAULT_VERSION; } JSONObject arguments = jsonRepresentation.getJsonObject(); return new JsonRepresentation( _service.getTranslation(arguments) ); } catch ( Exception e) { throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Unable to translate text. Check parameters.", e); } } /**
@Override public Collection<ResponseEntity> call(final String path, final JSONArray inputJson) throws ServiceException { Collection<ResponseEntity> responses = new ArrayList<>(); Preconditions.checkNotNull(inputJson); ClientResource service = createClient(path); try { Representation result = service.put(new JsonRepresentation(inputJson)); JsonRepresentation jsonRepresentation = new JsonRepresentation(result); if (service.getStatus().equals(Status.SUCCESS_OK)) { JSONArray jsonArray = jsonRepresentation.getJsonArray(); for (int i = 0; i < jsonArray.length(); i++) { ResponseEntity response = JsonUtil.fromJson(jsonArray.getJSONObject(i), ResponseEntity.class); responses.add(response); } } else { JSONObject jsonResult = jsonRepresentation.getJsonObject(); LOGGER.error("Unexpected HTTP status code returned: {}", service.getStatus().getCode()); throwServiceException(jsonResult); } } catch (JSONException | IOException | ResourceException e) { throwServiceException(service, e); } return responses; }
jsonSource = (JsonRepresentation) source; } else { jsonSource = new JsonRepresentation(source); if (JSONArray.class.isAssignableFrom(target)) { try { result = target.cast(jsonSource.getJsonArray()); } catch (JSONException e) { IOException ioe = new IOException( result = target.cast(jsonSource.getJsonObject()); } catch (JSONException e) { IOException ioe = new IOException( result = target.cast(jsonSource.getJsonTokener()); } catch (JSONException e) { IOException ioe = new IOException(
@Get("json") public JsonRepresentation represent() { Version v = new Version(); JsonRepresentation sr = new JsonRepresentation("{\"version\":\""+v.getVersion()+ "\"}"); sr.setMediaType(MediaType.APPLICATION_JSON); return sr; }
/** * Get JSON text from the Representation entity * @param entity json * @return String or null */ private String getJSONText(JsonRepresentation entity) { try { return entity.getText(); } catch (Exception e) { return null; } } }
/** * Constructor from a JSON tokener. * * @param jsonTokener * The JSON tokener. */ public JsonRepresentation(JSONTokener jsonTokener) { super(MediaType.APPLICATION_JSON); init(jsonTokener); }
/** * Gets the wrapped JSON array or converts the wrapped representation if * needed. * * @return The converted JSON array. * @throws JSONException */ public JSONArray getJsonArray() throws JSONException { if (this.jsonValue != null) { return (JSONArray) this.jsonValue; } return new JSONArray(getJsonText()); }
JSONArray jsonArray = (JSONArray) this.jsonValue; if (isIndenting()) { result = jsonArray.toString(getIndentingSize()); } else { result = jsonArray.toString(); JSONObject jsonObject = (JSONObject) this.jsonValue; if (isIndenting()) { result = jsonObject.toString(getIndentingSize()); } else { result = jsonObject.toString();
@Get public Representation getMe() throws IOException, JSONException { Token token = (Token) getRequest().getAttributes().get( Token.class.getName()); if (token == null) { return new StringRepresentation("Token not found!"); } ProtectedClientResource me = new ProtectedClientResource( "https://graph.facebook.com/me"); me.setUseBodyMethod(true); me.setToken(token); JSONObject result = new JsonRepresentation(me.get()).getJsonObject(); return new StringRepresentation("Hello " + result.getString("name")); } }
Representation result = service.put(new JsonRepresentation(inputJson)); JsonRepresentation jsonRepresentation = new JsonRepresentation(result); JSONObject jsonResult = jsonRepresentation.getJsonObject(); throwServiceException(jsonResult); } else JSONArray jsonResult = jsonRepresentation.getJsonArray();
jsonSource = (JsonRepresentation) source; } else { jsonSource = new JsonRepresentation(source); if (JSONArray.class.isAssignableFrom(target)) { try { result = target.cast(jsonSource.getJsonArray()); } catch (JSONException e) { IOException ioe = new IOException( result = target.cast(jsonSource.getJsonObject()); } catch (JSONException e) { IOException ioe = new IOException( result = target.cast(jsonSource.getJsonTokener()); } catch (JSONException e) { IOException ioe = new IOException(
/** * Get JSON text from the Representation entity * @param entity json * @return String or null */ private String getJSONText(JsonRepresentation entity) { try { return entity.getText(); } catch (Exception e) { return null; } } }
/** * Constructor from a JSON object. * * @param jsonObject * The JSON object. */ public JsonRepresentation(JSONObject jsonObject) { super(MediaType.APPLICATION_JSON); init(jsonObject); }
/** * Gets the wrapped JSON object or converts the wrapped representation if * needed. * * @return The converted JSON object. * @throws JSONException */ public JSONObject getJsonObject() throws JSONException { if (this.jsonValue != null) { return (JSONObject) this.jsonValue; } return new JSONObject(getJsonText()); }
JSONArray jsonArray = (JSONArray) this.jsonValue; if (isIndenting()) { result = jsonArray.toString(getIndentingSize()); } else { result = jsonArray.toString(); JSONObject jsonObject = (JSONObject) this.jsonValue; if (isIndenting()) { result = jsonObject.toString(getIndentingSize()); } else { result = jsonObject.toString();
@Put("json") public Representation updateUserStatus(Representation representation) throws IOException, JSONException { JSONObject request = new JsonRepresentation(representation) .getJsonObject(); Object status = request.get("status"); if (status == null) { setStatus(Status.CLIENT_ERROR_BAD_REQUEST); return null; } User user = getRequest().getClientInfo().getUser(); getLogger().info("updateUserStatus: " + user.getIdentifier()); SampleUser sampleUser = OAuth2Sample.getSampleUserManager() .findUserById(user.getIdentifier()); if (sampleUser != null) { sampleUser.setStatus(status.toString()); } JSONObject result = new JSONObject(); result.put("status", status); return new JsonRepresentation(result); } }
@Override public Representation toRepresentation(Object source, Variant target, Resource resource) { Representation result = null; if (source instanceof JSONArray) { result = new JsonRepresentation((JSONArray) source); } else if (source instanceof JSONObject) { result = new JsonRepresentation((JSONObject) source); } else if (source instanceof JSONTokener) { result = new JsonRepresentation((JSONTokener) source); } return result; }