@Get public JSONObject getIp(JsonRepresentation j) throws JSONException { JSONObject request = j.getJsonObject(); . . . }
try { (...) } catch(ResourceException ex) { Representation responseRepresentation = clientResource.getResponseEntity(); JsonRepresentation jsonRepr = new JsonRepresentation(responseRepresentation); JSONObject errors = jsonRepr.getJsonObject(); }
@Put public void store(JsonRepresentation mailRep) throws JSONException { // Parse the JSON representation to get the mail properties JSONObject mailElt = mailRep.getJsonObject(); // Output the JSON element values System.out.println("Status: " + mailElt.getString("status")); System.out.println("Subject: " + mailElt.getString("subject")); System.out.println("Content: " + mailElt.getString("content")); System.out.println("Account URI: " + mailElt.getString("accountRef")); } }
ClientResource resource = (...) (...) Representation repr = resource.get(); JsonRepresentation jsonRepresentation = new JsonRepresentation(repr); JSONObject jsonObj = jsonRepresentation.getJsonObject();
String uri="http://itunes.apple.com/search?term=marc+jordan&media=music&entity=album"; ClientResource resource = new ClientResource(url); Representation rep = resource.get(); JsonRepresentation represent = new JsonRepresentation(rep); JSONObject jsonobject = represent.getJsonObject(); String jsonText = jsonobject.toString(); Gson gson = new Gson(); Albums response = gson.fromJson(jsonText, Albums.class);
ClientResource cr = new ClientResource("https://"+url); Representation repr = cr.get(); JsonRepresentation jsonRepr = new JsonRepresentation(repr); String value = jsonRepr.getJsonObject().get("MY_VALUE");
Filter filter = new Filter(getContext()) { @Override protected int beforeHandle(Request request, Response response) { try { JsonRepresentation jrep = new JsonRepresentation(request.getEntity()); JSONObject jo = jrep.getJsonObject(); String token = jo.getString("token"); System.out.println(token); request.setEntity(new JsonRepresentation(jo)); } catch (JSONException | IOException e) { throw new ResourceException(Status.SERVER_ERROR_INTERNAL); } return super.beforeHandle(request, response); } };
Filter filter = new Filter(getContext()) { @Override protected int beforeHandle(Request request, Response response) { try { JsonRepresentation jrep = new JsonRepresentation(request.getEntity()); String token = jrep.getJsonObject().getString("token"); System.out.println(token); request.setEntity(jrep); } catch (JSONException | IOException e) { throw new ResourceException(Status.SERVER_ERROR_INTERNAL); } return super.beforeHandle(request, response); } };
ClientResource cr = new ClientResource("http://..."); Representation representation = cr.get(); JsonRepresentation jsonRepresentation = new JsonRepresentation(representation); JSONObject jsonObject = jsonRepresentation.getJsonObject();
Representation entity = new ClientResource("your url").get(); JsonRepresentation jsonRepresentation = new JsonRepresentation(entity.getText()); JSONObject jsonObject = jsonRepresentation.getJsonObject();
ClientResource clientResource = new ClientResource(url); Request request = new Request(Method.POST, url); clientResource.setRequest(request); Form form = new Form(); form.set("foo", "barValue"); org.restlet.representation.Representation response = clientResource.post(form, MediaType.APPLICATION_JSON); Representation responseEntity = clientResource.getResponseEntity(); JsonRepresentation jsonRepresentation = new JsonRepresentation(responseEntity); JSONObject jsonObject = jsonRepresentation.getJsonObject(); String[] names = JSONObject.getNames(jsonObject); if (jsonObject.has("errorString")) { String error = jsonObject.optString("errorString"); }
@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")); } }
@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); } } /**
@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 Object call(final Class<?> clazz, final String path) throws ServiceException { ClientResource service = createClient(path); try { Representation result = service.delete(); if (service.getStatus().equals(Status.SUCCESS_NO_CONTENT)) { LOGGER.info("Successfully deleted {}", path); } else { JsonRepresentation jsonRepresentation = new JsonRepresentation(result); JSONObject jsonResult = jsonRepresentation.getJsonObject(); LOGGER.error("Unexpected HTTP status code returned: {}", service.getStatus().getCode()); ResponseEntity response = JsonUtil.fromJson(jsonResult, ResponseEntity.class); throw new ServiceException(response); } } catch (JSONException | IOException e) { LOGGER.error("Unexpected Exception", e); throw new ServiceException(e); } return null; }
@Override public <T> String encodeMetadata(MetadataDataType metadataDataType, T instance) throws ServiceException { String encodedRawValue; ClientResource service = createClient(MetadataEndpoints.encodeMetadata(metadataDataType)); try { Representation result = service.post(new StringRepresentation(instance.toString())); JsonRepresentation jsonRepresentation = new JsonRepresentation(result); JSONObject jsonResult = jsonRepresentation.getJsonObject(); if (service.getStatus().equals(Status.SUCCESS_OK)) { encodedRawValue = jsonResult.getString(Field.RAW_VALUE_FIELD); } else { LOGGER.error("Unexpected HTTP status code returned: {}", service.getStatus().getCode()); ResponseEntity response = JsonUtil.fromJson(jsonResult, ResponseEntity.class); throw new ServiceException(response); } } catch (JSONException | IOException e) { LOGGER.error("Unexpected Exception", e); throw new ServiceException(e); } return encodedRawValue; }
@Override public void sendPasswordResetEmail(String emailAddress) throws ServiceException { ClientResource service = createClient(AccountEndpoints.resetMyPassword()); try { Representation result = service .post(new StringRepresentation(emailAddress, MediaType.TEXT_PLAIN), MediaType.APPLICATION_JSON); JsonRepresentation jsonRepresentation = new JsonRepresentation(result); JSONObject jsonResult = jsonRepresentation.getJsonObject(); ResponseEntity responseEntity = JsonUtil.fromJson(jsonResult, ResponseEntity.class); if (!service.getStatus().equals(Status.SUCCESS_OK)) { LOGGER.error("Unexpected HTTP status code returned: " + service.getStatus().getCode()); throw new ServiceException(responseEntity); } else { LOGGER.debug(responseEntity.getMessage()); } } catch (JSONException | IOException e) { LOGGER.error("Unexpected exception", e); throw new ServiceException(e); } }
public Token requestToken(OAuthParameters parameters) throws OAuthException, IOException, JSONException { if (authenticationScheme == null) { // Use Body method setupBodyClientCredentials(parameters); } else { setChallengeResponse(authenticationScheme, clientId, clientSecret); } Representation input = parameters.toRepresentation(); getClientInfo().getAcceptedMediaTypes().add( new Preference<MediaType>(MediaType.APPLICATION_JSON)); JSONObject result = new JsonRepresentation(post(input)).getJsonObject(); if (result.has(ERROR)) { throw OAuthException.toOAuthException(result); } TokenResponse token = TokenResponse.parseResponse(result); if (token.scope == null) { // Should be identical to the scope requested by the client. token.scope = Scopes.parseScope(parameters.toForm().getFirstValue( SCOPE)); } return token; }
@Override public JSONObject decodeMetadata(MetadataDataType metadataDataType, JSONObject jsonObject) throws ServiceException { JSONObject jsonResult; ClientResource service = createClient(MetadataEndpoints.decodeMetadata(metadataDataType)); try { Representation result = service.post(new JsonRepresentation(jsonObject)); JsonRepresentation jsonRepresentation = new JsonRepresentation(result); jsonResult = jsonRepresentation.getJsonObject(); if (service.getStatus().equals(Status.SUCCESS_OK)) { return jsonResult; } else { LOGGER.error("Unexpected HTTP status code returned: {}", service.getStatus().getCode()); ResponseEntity response = JsonUtil.fromJson(jsonResult, ResponseEntity.class); throw new ServiceException(response); } } catch (JSONException | IOException e) { LOGGER.error("Unexpected Exception", e); throw new ServiceException(e); } }
@Override public boolean isRealmAvailable(String realm) throws ServiceException { boolean isAvailable = false; ClientResource service = createClient(RegistrationEndpoints.checkRealmAvailability(realm)); try { Representation result = service.get(); JsonRepresentation jsonRepresentation = new JsonRepresentation(result); JSONObject jsonResult = jsonRepresentation.getJsonObject(); ResponseEntity responseEntity = JsonUtil.fromJson(jsonResult, ResponseEntity.class); if (!service.getStatus().equals(Status.SUCCESS_OK)) { LOGGER.error("Unexpected HTTP status code returned: " + service.getStatus().getCode()); throw new ServiceException(responseEntity); } else { isAvailable = (responseEntity.getCode() == Result.OK.getCode()); } } catch (JSONException | IOException e) { LOGGER.error("Unexpected exception", e); throw new ServiceException(e); } return isAvailable; }