下面列出了org.osgi.framework.dto.ServiceReferenceDTO#org.restlet.data.Method 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Response createTask(String projectId, String taskId, String label, AnalysisExecutionMode mode,
List<String> metrics, String start, String end) {
Request request = new Request(Method.POST, "http://localhost:8182/analysis/task/create");
ObjectMapper mapper = new ObjectMapper();
ObjectNode n = mapper.createObjectNode();
n.put("analysisTaskId", taskId);
n.put("label", label);
n.put("type", mode.name());
n.put("startDate", start);
n.put("endDate", end);
n.put("projectId", projectId);
ArrayNode arr = n.putArray("metricProviders");
for (String m : metrics)
arr.add(m);
request.setEntity(n.toString(), MediaType.APPLICATION_JSON);
return client.handle(request);
}
@Test
public void testEclipse() {
Client client = new Client(Protocol.HTTP);
Request request = new Request(Method.POST, "http://localhost:8182/projects/import");
ObjectMapper mapper = new ObjectMapper();
ObjectNode n = mapper.createObjectNode();
n.put("url", "https://projects.eclipse.org/projects/modeling.epsilon");
request.setEntity(n.toString(), MediaType.APPLICATION_JSON);
Response response = client.handle(request);
validateResponse(response, 201);
System.out.println(response.getEntityAsText());
}
@Test
public void testGitHub() {
Client client = new Client(Protocol.HTTP);
Request request = new Request(Method.POST, "http://localhost:8182/projects/import");
ObjectMapper mapper = new ObjectMapper();
ObjectNode n = mapper.createObjectNode();
n.put("url", "https://github.com/jrwilliams/gif-hook");
request.setEntity(n.toString(), MediaType.APPLICATION_JSON);
Response response = client.handle(request);
validateResponse(response, 201);
System.out.println(response.getEntityAsText());
}
public static ZNRecord post(Client client, String url, String body)
throws IOException {
Reference resourceRef = new Reference(url);
Request request = new Request(Method.POST, resourceRef);
request.setEntity(body, MediaType.APPLICATION_ALL);
Response response = client.handle(request);
Assert.assertEquals(response.getStatus(), Status.SUCCESS_OK);
Representation result = response.getEntity();
StringWriter sw = new StringWriter();
if (result != null) {
result.write(sw);
}
String responseStr = sw.toString();
Assert.assertTrue(responseStr.toLowerCase().indexOf("error") == -1);
Assert.assertTrue(responseStr.toLowerCase().indexOf("exception") == -1);
ObjectMapper mapper = new ObjectMapper();
ZNRecord record = mapper.readValue(new StringReader(responseStr), ZNRecord.class);
return record;
}
protected void assertRequestFails(String url, Method method, MediaType preferred, Object object, OntopiaRestErrors expected) {
OntopiaTestResource cr = new OntopiaTestResource(method, getUrl(url), preferred);
try {
cr.request(object, Object.class);
Assert.fail("Expected Ontopia error " + expected.name() + ", but request succeeded");
} catch (OntopiaTestResourceException e) {
Error result = e.getError();
try {
Assert.assertNotNull("Expected error, found null", result);
Assert.assertEquals("Ontopia error code mismatch", expected.getCode(), result.getCode());
Assert.assertEquals("HTTP status code mismatch", expected.getStatus().getCode(), result.getHttpcode());
Assert.assertNotNull("Error message is empty", result.getMessage());
} catch (AssertionError ae) {
Assert.fail("Expected ontopia error " + expected.name() +
", but received [" + result.getHttpcode() + ":" + result.getCode() + ", " + result.getMessage() + "]");
}
} catch (ResourceException re) {
Assert.fail("Expected ontopia error " + expected.name() +
", but received [" + re.getStatus().getCode() + ":" + re.getStatus().getDescription() + "]");
}
}
@Override
public boolean writeRequest(Object requestObject, Request request) throws ResourceException
{
if (requestObject == null)
{
if (!Method.GET.equals(request.getMethod()))
request.setEntity(new EmptyRepresentation());
return true;
}
if (requestObject instanceof Representation)
{
request.setEntity((Representation) requestObject);
return true;
}
for (RequestWriter requestWriter : requestWriters)
{
if (requestWriter.writeRequest(requestObject, request))
return true;
}
return false;
}
@Test
public void getMethodReturnsSuccessStatus() throws Exception {
// Rarely used Teapot Code to check.
final Boolean successStatusIs200 = true;
final ServiceEnabledCommand operation = mockedOperation(HttpMethod.GET, successStatusIs200);
classUnderTest = new GeoWaveOperationServiceWrapper(operation, null);
classUnderTest.setResponse(new Response(null));
classUnderTest.setRequest(new Request(Method.GET, "foo.bar"));
classUnderTest.restGet();
Assert.assertEquals(
successStatusIs200,
classUnderTest.getResponse().getStatus().equals(Status.SUCCESS_OK));
}
public Response setProperty(String key, String value) {
Request request = new Request(Method.POST, "http://localhost:8182/platform/properties/create");
ObjectMapper mapper = new ObjectMapper();
ObjectNode n = mapper.createObjectNode();
n.put("key", key);
n.put("value", value);
request.setEntity(n.toString(), MediaType.APPLICATION_JSON);
return client.handle(request);
}
public Response importProject(String url) {
Request request = new Request(Method.POST, "http://localhost:8182/projects/import");
ObjectMapper mapper = new ObjectMapper();
ObjectNode n = mapper.createObjectNode();
n.put("url", url);
request.setEntity(n.toString(), MediaType.APPLICATION_JSON);
return client.handle(request);
}
public Response startTask(String taskId) {
Request request = new Request(Method.POST, "http://localhost:8182/analysis/task/start");
ObjectMapper mapper = new ObjectMapper();
ObjectNode n = mapper.createObjectNode();
n.put("analysisTaskId", taskId);
request.setEntity(n.toString(), MediaType.APPLICATION_JSON);
return client.handle(request);
}
@Test
public void testPostInsert() throws Exception {
Request request = new Request(Method.POST, "http://localhost:8182/projects/");
ObjectMapper mapper = new ObjectMapper();
ObjectNode p = mapper.createObjectNode();
p.put("name", "test");
p.put("shortName", "test-short");
p.put("description", "this is a description");
request.setEntity(p.toString(), MediaType.APPLICATION_JSON);
Client client = new Client(Protocol.HTTP);
Response response = client.handle(request);
System.out.println(response.getEntity().getText() + " " + response.isEntityAvailable());
validateResponse(response, 201);
// Now try again, it should fail
response = client.handle(request);
validateResponse(response, 409);
// Clean up
Mongo mongo = new Mongo();
DB db = mongo.getDB("scava");
DBCollection col = db.getCollection("projects");
BasicDBObject query = new BasicDBObject("name", "test");
col.remove(query);
mongo.close();
}
public Request getHealthCheck() {
String requestUrl = StringUtils.join(new String[]{
_baseUrl, "/health"
});
Request request = new Request(Method.GET, requestUrl);
return request;
}
private ZNRecord get(Client client, String url, ObjectMapper mapper) throws Exception {
Request request = new Request(Method.GET, new Reference(url));
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);
ZNRecord record = mapper.readValue(new StringReader(responseStr), ZNRecord.class);
return record;
}
public Request getTopicExternalViewRequestUrl(String topic) {
String requestUrl = StringUtils.join(new String[]{
_baseUrl, "/topics/", topic
});
Request request = new Request(Method.GET, requestUrl);
return request;
}
public Request getTopicDeleteRequestUrl(String topic, String src, String dst) {
String requestUrl = StringUtils.join(new String[]{
_baseUrl, "/topics/", topic, "?src=", src, "&dst=", dst
});
Request request = new Request(Method.DELETE, requestUrl);
return request;
}
public Request getTopicCreationRequestUrl(String topic, String src, String dst) {
String requestUrl = StringUtils.join(new String[]{
_baseUrl, "/topics/", topic, "?src=", src, "&dst=", dst
});
Request request = new Request(Method.POST, requestUrl);
return request;
}
public Request getTopicExpansionRequestUrl(String topic, String src, String dst, int numPartitions) {
String requestUrl = StringUtils.join(new String[]{
_baseUrl, "/topics/", topic, "?src=", src, "&dst=", dst, "&partitions=", String.valueOf(numPartitions)
});
Request request = new Request(Method.PUT, requestUrl);
return request;
}
public Request postSetControllerRebalance(String srcCluster, String dstCluster, boolean enabled) {
String requestUrl = StringUtils.join(new String[]{
_baseUrl, "/admin/controller_autobalancing?srcCluster="+ srcCluster + "&dstCluster=" + dstCluster
+ "&enabled=" + enabled
});
Request request = new Request(Method.POST, requestUrl);
return request;
}
public Request postSetControllerRebalance(boolean enabled) {
String requestUrl = StringUtils.join(new String[]{
_baseUrl, "/admin/controller_autobalancing?enabled=" + enabled
});
Request request = new Request(Method.POST, requestUrl);
return request;
}
@Test
public void testGetResources() throws IOException {
final String clusterName = "TestTagAwareness_testGetResources";
final String TAG = "tag";
final String URL_BASE =
"http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/resourceGroups";
_gSetupTool.addCluster(clusterName, true);
HelixAdmin admin = _gSetupTool.getClusterManagementTool();
// Add a tagged resource
IdealState taggedResource = new IdealState("taggedResource");
taggedResource.setInstanceGroupTag(TAG);
taggedResource.setStateModelDefRef("OnlineOffline");
admin.addResource(clusterName, taggedResource.getId(), taggedResource);
// Add an untagged resource
IdealState untaggedResource = new IdealState("untaggedResource");
untaggedResource.setStateModelDefRef("OnlineOffline");
admin.addResource(clusterName, untaggedResource.getId(), untaggedResource);
// Now make a REST call for all resources
Reference resourceRef = new Reference(URL_BASE);
Request request = new Request(Method.GET, resourceRef);
Response response = _gClient.handle(request);
ZNRecord responseRecord =
ClusterRepresentationUtil.JsonToObject(ZNRecord.class, response.getEntityAsText());
// Ensure that the tagged resource has information and the untagged one doesn't
Assert.assertNotNull(responseRecord.getMapField("ResourceTags"));
Assert
.assertEquals(TAG, responseRecord.getMapField("ResourceTags").get(taggedResource.getId()));
Assert.assertFalse(responseRecord.getMapField("ResourceTags").containsKey(
untaggedResource.getId()));
}
public Request getTopicDeleteRequestUrl(String topic) {
String requestUrl = StringUtils.join(new String[]{
_baseUrl, "/topics/", topic
});
Request request = new Request(Method.DELETE, requestUrl);
return request;
}
public Request getWorkloadInfoUrl() {
String requestUrl = StringUtils.join(new String[]{
_baseUrl, "/admin/workloadinfo"
});
Request request = new Request(Method.GET, requestUrl);
return request;
}
public Request postBlacklistRequestUrl(String topic, String partition, String opt) {
String requestUrl = StringUtils.join(new String[]{
_baseUrl, String.format("/blacklist?topic=%s&partition=%s&opt=%s", topic, partition, opt)
});
Request request = new Request(Method.POST, requestUrl);
return request;
}
public Request getHealthCheck() {
String requestUrl = StringUtils.join(new String[]{
_baseUrl, "/health"
});
Request request = new Request(Method.GET, requestUrl);
return request;
}
public Request getTopicExternalViewRequestUrl(String topic) {
String requestUrl = StringUtils.join(new String[]{
_baseUrl, "/topics/", topic
});
Request request = new Request(Method.GET, requestUrl);
return request;
}
public Request getTopicDeleteRequestUrl(String topic, String src, String dst) {
String requestUrl = StringUtils.join(new String[]{
_baseUrl, "/topics/", topic, "?src=", src, "&dst=", dst
});
Request request = new Request(Method.DELETE, requestUrl);
return request;
}
public Request getTopicCreationRequestUrl(String topic, String src, String dst) {
String requestUrl = StringUtils.join(new String[]{
_baseUrl, "/topics/", topic, "?src=", src, "&dst=", dst
});
Request request = new Request(Method.POST, requestUrl);
return request;
}
/** Called by Restlet to get the response body */
@Override
public void write(OutputStream outputStream) throws IOException {
if (getRequest().getMethod() != Method.HEAD) {
QueryResponseWriterUtil.writeQueryResponse(outputStream, responseWriter, solrRequest, solrResponse, contentType);
}
}
/**
* @see org.osgi.rest.client.RestClient#getFrameworkStartLevel()
*/
public FrameworkStartLevelDTO getFrameworkStartLevel() throws Exception {
final Representation repr = new ClientResource(Method.GET,
baseUri.resolve("framework/startlevel"))
.get(FRAMEWORK_STARTLEVEL);
return DTOReflector.getDTO(FrameworkStartLevelDTO.class, repr);
}
/**
* @see org.osgi.rest.client.RestClient#setFrameworkStartLevel(org.osgi.dto.framework
* .startlevel.FrameworkStartLevelDTO)
*/
public void setFrameworkStartLevel(final FrameworkStartLevelDTO startLevel)
throws Exception {
new ClientResource(Method.PUT, baseUri.resolve("framework/startlevel")).put(
DTOReflector.getJson(FrameworkStartLevelDTO.class, startLevel),
FRAMEWORK_STARTLEVEL);
}