Refine search
Form form = new Form(); form.add("x", "foo"); form.add("y", "bar"); ClientResource resource = new ClientResource("http://localhost:8080/someresource"); Response response = resource.post(form.getWebRepresentation()); if (response.getStatus().isSuccess()) { System.out.println("Success! " + response.getStatus()); System.out.println(response.getEntity().getText()); } else { System.out.println("ERROR! " + response.getStatus()); System.out.println(response.getEntity().getText()); }
ClientResource cr = new ClientResource("..."); Series<Header> headers = cr.getRequestAttributes().get( "org.restlet.http.headers"); headers.set("<header-name>", "<header-value>");
ClientResource cr = new ClientResource("..."); Series<Header> headers = cr.getHeaders(); headers.set("<header-name>", "<header-value>");
@Override protected Representation get() throws ResourceException { // Create the mail bean MailRepresentation mail = new MailRepresentation(); mail.setStatus("received"); mail.setSubject("Message to self"); mail.setContent("Doh!"); mail.setAccountRef(new Reference(getReference(), "..").getTargetRef() .toString()); // Load the FreeMarker template Representation mailFtl = new ClientResource( LocalReference.createClapReference(getClass().getPackage()) + "/Mail.ftl").get(); // Wraps the bean with a FreeMarker representation return new TemplateRepresentation(mailFtl, mail, MediaType.TEXT_HTML); }
@Override public void challenge(Response response, boolean stale) { // Load the FreeMarker template Representation ftl = new ClientResource( LocalReference.createClapReference(getClass().getPackage()) + "/Login.ftl").get(); // Wraps the bean with a FreeMarker representation response.setEntity(new TemplateRepresentation(ftl, response .getRequest().getResourceRef(), MediaType.TEXT_HTML)); response.setStatus(Status.CLIENT_ERROR_UNAUTHORIZED); }
/** * Tests whether the RDF/JSON page when there are no annotations is correct. */ @Test public void testAnnotationListEmptyJson() throws Exception { final ClientResource getResource = new ClientResource(this.getUrl(this.propertyUtil.get(OasProps.PROP_BULK_FETCH_ANNOTATION_PATH, OasProps.DEF_BULK_FETCH_ANNOTATION_PATH))); final Representation results = getResource.get(RestletUtilMediaType.APPLICATION_RDF_JSON); // Expecting HTTP 200 OK response Assert.assertEquals(200, getResource.getResponse().getStatus().getCode()); // Expecting application/json MIME type for response Assert.assertEquals(RestletUtilMediaType.APPLICATION_RDF_JSON.getName(), results.getMediaType().getName()); this.assertRdf(results.getStream(), RDFFormat.RDFJSON, 0); }
@Test public void testGetWhoAmIUnauthenticated() throws Exception { final ClientResource creationClientResource = new ClientResource(this.getUrl(this.propertyUtil.get(OasProps.PROP_WHOAMI_PATH, OasProps.DEF_WHOAMI_PATH))); try { creationClientResource.get(); } catch(final ResourceException rex) { Assert.assertEquals(Status.CLIENT_ERROR_UNAUTHORIZED, rex.getStatus()); } }
public static void main(String[] args) throws Exception { ClientResource mailClient = new ClientResource( "http://localhost:8111/accounts/chunkylover53/mails/123"); Representation mailRepresentation = mailClient.get(); mailClient.put(mailRepresentation); }
@Test public void testOntologyBrowserDirectJson() throws Exception { final ClientResource ontologyBrowserClientResource = new ClientResource(this.getUrl(this.propertyUtil.get(OasProps.PROP_ONTOLOGY_BROWSER_PATH, OasProps.DEF_ONTOLOGY_BROWSER_PATH))); ontologyBrowserClientResource.getReference().addQueryParameter("ontologyUri", this.ontologyUri); ontologyBrowserClientResource.getReference().addQueryParameter("ontologyVersionUri", this.ontologyVersionUri); ontologyBrowserClientResource.getReference().addQueryParameter("directChildrenOnly", "true"); final Representation representation = ontologyBrowserClientResource.get(MediaType.APPLICATION_JSON); Assert.assertNotNull(representation); Assert.assertEquals(Status.SUCCESS_OK.getCode(), ontologyBrowserClientResource.getStatus().getCode()); // this.log.trace("result={}", representation.getText()); }
public static ClientResource createAuthenticatedClientResource(String url, String userName, String password) { ClientResource cr = new ClientResource(url); cr.setNext(client); cr.accept(MediaType.APPLICATION_JSON); if (!StringUtils.isNullOrEmpty(userName) && !StringUtils.isNullOrEmpty(password)) { cr.setChallengeResponse(ChallengeScheme.HTTP_BASIC, userName, password); } return cr; }
@Override public List<Car> list() { Client client = new Client(new Context(), Protocol.HTTPS); Series<Parameter> parameters = client.getContext().getParameters(); parameters.add("truststorePath", System.getProperty("javax.net.ssl.trustStore")); ClientResource clientResource = new ClientResource("https://localhost:8043/api/cars/cars"); clientResource.setNext(client); ChallengeResponse challenge = new ChallengeResponse(ChallengeScheme.HTTP_OAUTH_BEARER); challenge.setRawValue(Request.getCurrent().getAttributes().getOrDefault("token", "").toString()); clientResource.setChallengeResponse(challenge); CarServiceInterface carServiceInterface = clientResource.wrap(CarServiceInterface.class); Car[] allCars = carServiceInterface.getAllCars(); try { client.stop(); } catch (Exception e) { throw new RuntimeException(e); } return asList(allCars); } }
@SuppressWarnings("unchecked") private void writeRequestInfoToLog(ClientResource clientResource) { ConcurrentMap<String, Object> attrs = clientResource.getRequest().getAttributes(); Series<Header> headers = (Series<Header>) attrs.get(HeaderConstants.ATTRIBUTE_HEADERS); logger.info("Request to Poulpe: requested URI - {}, request headers - {}, request body - {}", new Object[]{clientResource.getRequest().getResourceRef(), headers, clientResource.getRequest()}); }
public static void main(String[] args) throws IOException, ResourceException { ClientResource itemsResource = new ClientResource( "http://localhost:8111/firstResource/items"); ClientResource itemResource = null; Representation r = itemsResource.post(getRepresentation(item)); itemResource = new ClientResource(r.getLocationRef()); } catch (ResourceException e) { System.out.println("Error status: " + e.getStatus()); System.out.println("Error message: " + e.getMessage()); itemsResource.getResponseEntity().exhaust(); itemResource.put(getRepresentation(item)); itemResource.delete();
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()); } }
/** * Tests that an error occurs when trying to create a user without submitting a form */ @Test public void testErrorUpdateUserPostNoContent() throws Exception { try { final ClientResource creationClientResource = new ClientResource(this.getUrl(this.propertyUtil.get(OasProps.PROP_UPDATE_USER_PATH, OasProps.PROP_UPDATE_USER_PATH))); final FormDataSet form = new FormDataSet(); form.setMultipart(false); form.setMediaType(MediaType.APPLICATION_WWW_FORM); creationClientResource.post(form); Assert.fail("Did not receive expected exception"); } catch(final ResourceException rex) { Assert.assertEquals(Status.CLIENT_ERROR_BAD_REQUEST, rex.getStatus()); } }
/** * Posts an object entity. Automatically serializes the object using the * {@link org.restlet.service.ConverterService}. * * @param entity * The object entity to post. * @return The optional result entity. * @throws ResourceException * @see <a * href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.5">HTTP * POST method</a> */ public Representation post(Object entity) throws ResourceException { try { return post(toRepresentation(entity)); } catch (IOException e) { throw new ResourceException(e); } }
@Override public Representation getRepresentation(Status status, Request request, Response response) { // Create the data model Map<String, String> dataModel = new ConcurrentHashMap<String, String>(); dataModel.put("applicationName", Application.getCurrent().getName()); dataModel.put("statusDescription", response.getStatus() .getDescription()); dataModel.put("statusReasonPhrase", response.getStatus() .getReasonPhrase()); // Load the FreeMarker template Representation mailFtl = new ClientResource( LocalReference.createClapReference(getClass().getPackage()) + "/MailStatus.ftl").get(); // Wraps the bean with a FreeMarker representation return new TemplateRepresentation(mailFtl, dataModel, MediaType.TEXT_HTML); }
/** * Releases the resource by stopping any connector automatically created and * associated to the "next" property (see {@link #getNext()} method. */ @Override protected void doRelease() throws ResourceException { if ((getNext() != null) && this.nextCreated) { if (getNext() instanceof Restlet) { try { ((Restlet) getNext()).stop(); } catch (Exception e) { throw new ResourceException(e); } } setNext(null); } }
@Test public void testGetAuditLogAuthenticatedNonAdminRdfJson() throws Exception { final ClientResource creationClientResource = new ClientResource(this.getUrl(this.propertyUtil.get(OasProps.PROP_AUDITLOG_PATH, OasProps.DEF_AUDITLOG_PATH))); try { RestletTestUtils.doTestAuthenticatedRequest(creationClientResource, Method.GET, null, RestletUtilMediaType.APPLICATION_RDF_JSON, Status.SUCCESS_OK, this.testNoAdminPrivileges); Assert.fail("Did not receive expected exception"); } catch(final ResourceException rex) { Assert.assertEquals(Status.CLIENT_ERROR_UNAUTHORIZED, rex.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 { 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(); } }