use of org.restlet.representation.Representation in project helix by apache.
the class TestClusterManagementWebapp method verifyEnableCluster.
void verifyEnableCluster() throws Exception {
System.out.println("START: verifyEnableCluster()");
String httpUrlBase = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/Controller";
Map<String, String> paramMap = new HashMap<String, String>();
paramMap.put(JsonParameters.MANAGEMENT_COMMAND, ClusterSetup.enableCluster);
paramMap.put(JsonParameters.ENABLED, "" + false);
Reference resourceRef = new Reference(httpUrlBase);
Request request = new Request(Method.POST, resourceRef);
request.setEntity(JsonParameters.JSON_PARAMETERS + "=" + ClusterRepresentationUtil.ObjectToJson(paramMap), MediaType.APPLICATION_ALL);
Response response = _gClient.handle(request);
Representation result = response.getEntity();
StringWriter sw = new StringWriter();
result.write(sw);
System.out.println(sw.toString());
// verify pause znode exists
String pausePath = PropertyPathBuilder.pause(clusterName);
System.out.println("pausePath: " + pausePath);
boolean exists = _gZkClient.exists(pausePath);
Assert.assertTrue(exists, pausePath + " should exist");
// Then enable it
paramMap.put(JsonParameters.ENABLED, "" + true);
request = new Request(Method.POST, resourceRef);
request.setEntity(JsonParameters.JSON_PARAMETERS + "=" + ClusterRepresentationUtil.ObjectToJson(paramMap), MediaType.APPLICATION_ALL);
response = _gClient.handle(request);
result = response.getEntity();
sw = new StringWriter();
result.write(sw);
System.out.println(sw.toString());
// verify pause znode doesn't exist
exists = _gZkClient.exists(pausePath);
Assert.assertFalse(exists, pausePath + " should be removed");
System.out.println("END: verifyEnableCluster()");
}
use of org.restlet.representation.Representation in project helix by apache.
the class TestHelixAdminScenariosRest method deleteUrl.
void deleteUrl(String url, boolean hasException) throws IOException {
Reference resourceRef = new Reference(url);
Request request = new Request(Method.DELETE, resourceRef);
Response response = _gClient.handle(request);
Representation result = response.getEntity();
StringWriter sw = new StringWriter();
result.write(sw);
Assert.assertTrue(hasException == sw.toString().toLowerCase().contains("exception"));
}
use of org.restlet.representation.Representation in project openems by OpenEMS.
the class DeviceNatureRestlet method handle.
@Override
public void handle(Request request, Response response) {
super.handle(request, response);
// call handler methods
if (request.getMethod().equals(Method.GET)) {
Representation entity = get();
response.setEntity(entity);
}
}
use of org.restlet.representation.Representation in project qi4j-sdk by Qi4j.
the class LinksResponseWriter method writeResponse.
@Override
public boolean writeResponse(final Object result, final Response response) throws ResourceException {
if (result instanceof Link) {
MediaType type = getVariant(response.getRequest(), ENGLISH, supportedLinkMediaTypes).getMediaType();
if (MediaType.APPLICATION_JSON.equals(type)) {
response.setEntity(new StringRepresentation(((Link) result).toString(), MediaType.APPLICATION_JSON));
return true;
} else {
response.setStatus(Status.REDIRECTION_TEMPORARY);
Link link = (Link) result;
Reference reference = new Reference(response.getRequest().getResourceRef(), link.href().get());
response.setLocationRef(reference);
return true;
}
} else if (result instanceof Links) {
MediaType type = getVariant(response.getRequest(), ENGLISH, supportedLinksMediaTypes).getMediaType();
Representation rep;
if (MediaType.APPLICATION_JSON.equals(type)) {
rep = createJsonRepresentation((Links) result);
} else if (MediaType.TEXT_HTML.equals(type)) {
rep = createTextHtmlRepresentation(result, response);
} else if (MediaType.APPLICATION_ATOM.equals(type)) {
rep = createAtomRepresentation(result, response);
} else {
return false;
}
response.setEntity(rep);
return true;
}
return false;
}
use of org.restlet.representation.Representation in project qi4j-sdk by Qi4j.
the class ValueCompositeResponseWriter method writeResponse.
@Override
public boolean writeResponse(final Object result, final Response response) throws ResourceException {
if (result instanceof ValueComposite) {
MediaType type = getVariant(response.getRequest(), ENGLISH, supportedMediaTypes).getMediaType();
if (MediaType.APPLICATION_JSON.equals(type)) {
StringRepresentation representation = new StringRepresentation(valueSerializer.serialize(result), MediaType.APPLICATION_JSON);
response.setEntity(representation);
return true;
} else if (MediaType.TEXT_HTML.equals(type)) {
Representation rep = new WriterRepresentation(MediaType.TEXT_HTML) {
@Override
public void write(Writer writer) throws IOException {
// Look for type specific template
Template template;
try {
template = cfg.getTemplate("/rest/template/" + result.getClass().getInterfaces()[0].getSimpleName() + ".htm");
} catch (Exception e) {
// Use default
template = cfg.getTemplate("value.htm");
}
Map<String, Object> context = new HashMap<String, Object>();
context.put("request", response.getRequest());
context.put("response", response);
context.put("result", result);
context.put("util", this);
try {
template.process(context, writer);
} catch (TemplateException e) {
throw new IOException(e);
}
}
public boolean isSequence(Object obj) {
return obj instanceof Collection;
}
};
response.setEntity(rep);
return true;
}
}
return false;
}
Aggregations