private void closeRestletConnection(ClientResource clientResource) { try { clientResource.getResponseEntity().exhaust(); } catch (IOException e) { logger.warn("Error closing connection: {}", e.getMessage() ); } }
/** * Returns a Properties instance loaded from the given URI. * * @param propertiesUri * The URI of the properties file. * @return A Properties instance loaded from the given URI. * @throws IOException */ public static Properties getProperties(String propertiesUri) throws IOException { ClientResource resource = new ClientResource(propertiesUri); try { resource.get(); } catch (Exception e) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("Cannot access to the configuration file: \""); stringBuilder.append(propertiesUri); stringBuilder.append("\""); throw new IllegalArgumentException(stringBuilder.toString()); } Properties properties = new Properties(); properties.load(resource.getResponseEntity().getStream()); return properties; }
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"); }
/** * Prints the resource's representation. * * @param clientResource * The Restlet client resource. * @throws IOException * @throws ResourceException */ public static void get(ClientResource clientResource) throws IOException, ResourceException { try { clientResource.get().write(System.out); } catch (ResourceException e) { System.out.println("Error status: " + e.getStatus()); System.out.println("Error message: " + e.getMessage()); // Consume the response's entity which releases the connection clientResource.getResponseEntity().exhaust(); } }
/** * Gets authentication result from response entity. * * @param clientResource response container * @return map with user details * @throws org.jtalks.jcommune.plugin.api.exceptions.NoConnectionException * */ private Map<String, String> getAuthResult(ClientResource clientResource) throws NoConnectionException, JAXBException, IOException { if (clientResource.getStatus().getCode() == Status.SUCCESS_OK.getCode() && clientResource.getResponseEntity() != null) { return parseUserDetails(clientResource.getResponseEntity()); } else if (clientResource.getStatus().getCode() == Status.CLIENT_ERROR_NOT_FOUND.getCode()) { return Collections.emptyMap(); } else { throw new NoConnectionException(clientResource.getStatus().toString()); } }
public static void main(String[] args) { // Prepare the request ClientResource resource = new ClientResource("http://localhost:8182/"); // Add the client authentication to the call ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC; ChallengeResponse authentication = new ChallengeResponse(scheme, "scott", "tiger"); resource.setChallengeResponse(authentication); try { // Send the HTTP GET request resource.get(); // Output the response entity on the JVM console resource.getResponseEntity().write(System.out); } catch (Exception e) { if (Status.CLIENT_ERROR_UNAUTHORIZED.equals(resource.getStatus())) { // Unauthorized access System.out .println("Access unauthorized by the server, check your credentials"); } else { // Unexpected status System.out.println("An unexpected status was returned: " + resource.getStatus()); } } } }
/** * Prints the resource's representation. * * @param clientResource * The Restlet client resource. * @throws IOException * @throws ResourceException */ public static void get(ClientResource clientResource) throws IOException, ResourceException { clientResource.get(); if (clientResource.getStatus().isSuccess() && clientResource.getResponse().isEntityAvailable()) { clientResource.getResponseEntity().write(System.out); } }
public static void main(String[] args) throws Exception { // Prepare the request ClientResource resource = new ClientResource("http://localhost:8111/"); // Add the client authentication to the call ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC; ChallengeResponse authentication = new ChallengeResponse(scheme, "scott", "tiger"); resource.setChallengeResponse(authentication); // Send the HTTP GET request resource.get(); if (resource.getStatus().isSuccess()) { // Output the response entity on the JVM console resource.getResponseEntity().write(System.out); } else if (resource.getStatus() .equals(Status.CLIENT_ERROR_UNAUTHORIZED)) { // Unauthorized access System.out .println("Access authorized by the server, check your credentials"); } else { // Unexpected status System.out.println("An unexpected status was returned: " + resource.getStatus()); } }
/** * Gets errors from response if request wasn't successful, otherwise return null. * * @param clientResource response container * @param locale locale * @return errors * @throws org.jtalks.jcommune.plugin.api.exceptions.NoConnectionException * * @throws java.io.IOException */ private Map<String, String> getRegistrationResult(ClientResource clientResource, Locale locale) throws NoConnectionException, IOException, JAXBException, UnexpectedErrorException { if (clientResource.getStatus().getCode() == Status.SUCCESS_OK.getCode() && clientResource.getResponseEntity() != null) { return Collections.emptyMap(); } else if (clientResource.getStatus().getCode() == Status.CLIENT_ERROR_BAD_REQUEST.getCode()) { return parseErrors(clientResource.getResponseEntity(), locale); } else if (clientResource.getStatus().getCode() == Status.SERVER_ERROR_INTERNAL.getCode()) { throw new UnexpectedErrorException(); } else { throw new NoConnectionException(clientResource.getStatus().toString()); } }
if (cr.getResponseEntity() != null && cr.getResponseEntity().isAvailable()) { try { cr.getResponseEntity().write(System.out); System.out.println(); } catch (IOException e) {
if (cr.getResponseEntity() != null && cr.getResponseEntity().isAvailable()) { try { cr.getResponseEntity().write(System.out); System.out.println(); } catch (IOException e) {
if (cr.getResponseEntity() != null && cr.getResponseEntity().isAvailable()) { try { cr.getResponseEntity().write(System.out); System.out.println(); } catch (IOException e) {
if (cr.getResponseEntity() != null && cr.getResponseEntity().isAvailable()) { try { cr.getResponseEntity().write(System.out); System.out.println(); } catch (IOException e) {
if (cr.getResponseEntity() != null && cr.getResponseEntity().isAvailable()) { try { cr.getResponseEntity().write(System.out); System.out.println(); } catch (IOException e) {
itemsResource.getResponseEntity().exhaust();