use of org.restlet.Request in project camel by apache.
the class RestletRouteBuilderTest method testUnhandledConsumer.
@Test
public void testUnhandledConsumer() throws IOException {
Client client = new Client(Protocol.HTTP);
Response response = client.handle(new Request(Method.POST, "http://localhost:" + portNum + "/orders/99991/6"));
// expect error status as no Restlet consumer to handle POST method
assertEquals(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED, response.getStatus());
assertNotNull(response.getEntity().getText());
}
use of org.restlet.Request in project xwiki-platform by xwiki.
the class AnnotationsRESTResource method doGetAnnotatedContent.
/**
* @param wiki the wiki of the document to get annotations for
* @param space the space of the document to get annotations for
* @param page the name of the document to get annotation for
* @return annotations of a given XWiki page. Note that we're returning a response holding the AnnotatedContent
* instead of an AnnotatedContent object because we need to be able to set custom expire fields to prevent
* IE from caching this resource.
* @throws XWikiRestException when failing to parse space
*/
@GET
public Response doGetAnnotatedContent(@PathParam("spaceName") String space, @PathParam("pageName") String page, @PathParam("wikiName") String wiki) throws XWikiRestException {
try {
DocumentReference documentReference = new DocumentReference(wiki, parseSpaceSegments(space), page);
// Initialize the context with the correct value.
updateContext(documentReference);
String documentName = referenceSerializer.serialize(documentReference);
// check access to this function
if (!annotationRightService.canViewAnnotatedTarget(documentName, getXWikiUser())) {
throw new WebApplicationException(Status.UNAUTHORIZED);
}
// Manually construct the Annotation request entity
// Historically it used to be passed as method argument, but this only worked because of a bug in
// earlier version of Restlet (1.1.x) ; the JAX-RS specification explicitly states that such entity
// parameters "[are] mapped from the request entity body.", and thus cannot be passed to a GET resource.
// See paragraph 3.3.2.1 "Entity Parameters" of JAX-RS 1.1 specification.
Form form = Request.getCurrent().getResourceRef().getQueryAsForm();
AnnotationRequest request = new AnnotationRequest();
AnnotationFieldCollection fields = new AnnotationFieldCollection();
List<AnnotationField> annotationFields = new ArrayList<AnnotationField>();
AnnotationRequest.Request requestedFields = new AnnotationRequest.Request();
for (String name : form.getNames()) {
if (StringUtils.startsWith(name, ANNOTATION_REQUEST_FILTER_PARAMETER_PREFIX)) {
for (String value : form.getValuesArray(name)) {
AnnotationField field = new AnnotationField();
field.setName(StringUtils.substringAfter(name, ANNOTATION_REQUEST_FILTER_PARAMETER_PREFIX));
field.setValue(value);
annotationFields.add(field);
}
} else if (StringUtils.equals(name, ANNOTATION_REQUEST_REQUESTED_FIELD_PARAMETER)) {
requestedFields.getFields().addAll(Arrays.asList(form.getValuesArray(name)));
}
}
request.setRequest(requestedFields);
fields.getFields().addAll(annotationFields);
request.setFilter(fields);
AnnotationResponse response = getSuccessResponseWithAnnotatedContent(documentName, request);
// make this content expire now because cacheControl is not implemented in this version of restlet
return Response.ok(response).expires(new Date()).build();
} catch (AnnotationServiceException e) {
getLogger().error(e.getMessage(), e);
return Response.ok(getErrorResponse(e)).build();
} catch (XWikiException e) {
getLogger().error(e.getMessage(), e);
return Response.ok(getErrorResponse(e)).build();
}
}
use of org.restlet.Request in project helix by apache.
the class AdminTestHelper method delete.
public static void delete(Client client, String url) throws IOException {
Reference resourceRef = new Reference(url);
Request request = new Request(Method.DELETE, resourceRef);
Response response = client.handle(request);
Assert.assertEquals(response.getStatus(), Status.SUCCESS_NO_CONTENT);
}
use of org.restlet.Request in project helix by apache.
the class TestClusterManagementWebapp method postConfig.
private void postConfig(Client client, String url, ObjectMapper mapper, String command, String configs) throws Exception {
Map<String, String> params = new HashMap<String, String>();
params.put(JsonParameters.MANAGEMENT_COMMAND, command);
params.put(JsonParameters.CONFIGS, configs);
Request request = new Request(Method.POST, new Reference(url));
request.setEntity(JsonParameters.JSON_PARAMETERS + "=" + ClusterRepresentationUtil.ObjectToJson(params), MediaType.APPLICATION_ALL);
Response response = client.handle(request);
Representation result = response.getEntity();
StringWriter sw = new StringWriter();
result.write(sw);
String responseStr = sw.toString();
Assert.assertTrue(responseStr.toLowerCase().indexOf("error") == -1);
Assert.assertTrue(responseStr.toLowerCase().indexOf("exception") == -1);
}
use of org.restlet.Request in project helix by apache.
the class TestClusterManagementWebapp method verifyEnableInstance.
void verifyEnableInstance() throws JsonGenerationException, JsonMappingException, IOException {
String httpUrlBase = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/instances/" + instance1 + "_" + instancePort;
Map<String, String> paraMap = new HashMap<String, String>();
// Add 1 instance
paraMap.put(JsonParameters.ENABLED, "" + false);
paraMap.put(JsonParameters.MANAGEMENT_COMMAND, ClusterSetup.enableInstance);
Reference resourceRef = new Reference(httpUrlBase);
Request request = new Request(Method.POST, resourceRef);
request.setEntity(JsonParameters.JSON_PARAMETERS + "=" + ClusterRepresentationUtil.ObjectToJson(paraMap), MediaType.APPLICATION_ALL);
Response response = _gClient.handle(request);
Representation result = response.getEntity();
StringWriter sw = new StringWriter();
result.write(sw);
// System.out.println(sw.toString());
ObjectMapper mapper = new ObjectMapper();
ZNRecord r = mapper.readValue(new StringReader(sw.toString()), ZNRecord.class);
AssertJUnit.assertTrue(r.getSimpleField(InstanceConfigProperty.HELIX_ENABLED.toString()).equals("" + false));
// Then enable it
paraMap.put(JsonParameters.ENABLED, "" + true);
request = new Request(Method.POST, resourceRef);
request.setEntity(JsonParameters.JSON_PARAMETERS + "=" + ClusterRepresentationUtil.ObjectToJson(paraMap), MediaType.APPLICATION_ALL);
response = _gClient.handle(request);
result = response.getEntity();
sw = new StringWriter();
result.write(sw);
// System.out.println(sw.toString());
mapper = new ObjectMapper();
r = mapper.readValue(new StringReader(sw.toString()), ZNRecord.class);
AssertJUnit.assertTrue(r.getSimpleField(InstanceConfigProperty.HELIX_ENABLED.toString()).equals("" + true));
}
Aggregations