Java Code Examples for org.restlet.representation.Representation#write()

The following examples show how to use org.restlet.representation.Representation#write() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AdminTestHelper.java    From helix with Apache License 2.0 7 votes vote down vote up
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;
}
 
Example 2
Source File: AdminTestHelper.java    From helix with Apache License 2.0 6 votes vote down vote up
public static ZNRecord get(Client client, String url) throws IOException {
  Reference resourceRef = new Reference(url);
  Request request = new Request(Method.GET, resourceRef);
  Response response = client.handle(request);
  Assert.assertEquals(response.getStatus(), Status.SUCCESS_OK);
  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);
  ObjectMapper mapper = new ObjectMapper();
  ZNRecord record = mapper.readValue(new StringReader(responseStr), ZNRecord.class);
  return record;
}
 
Example 3
Source File: TestClusterManagementWebapp.java    From helix with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 4
Source File: TestDeploy.java    From FoxBPM with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
	ClientResource client = new ClientResource("http://127.0.0.1:8082/model/deployments");
	client.setChallengeResponse(ChallengeScheme.HTTP_BASIC,"111", "111");
	
	InputStream input = TestDeploy.class.getClassLoader().getResourceAsStream("FirstFoxbpm.zip");
	Representation deployInput = new InputRepresentation(input);
	Representation result = client.post(deployInput);
	try {
		result.write(System.out);
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
Example 5
Source File: TestHelixAdminScenariosRest.java    From helix with Apache License 2.0 5 votes vote down vote up
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"));
}
 
Example 6
Source File: TestHelixAdminScenariosRest.java    From helix with Apache License 2.0 5 votes vote down vote up
String getUrl(String url) throws IOException {
  Reference resourceRef = new Reference(url);
  Request request = new Request(Method.GET, resourceRef);
  Response response = _gClient.handle(request);
  Representation result = response.getEntity();
  StringWriter sw = new StringWriter();
  result.write(sw);
  return sw.toString();
}
 
Example 7
Source File: TestClusterManagementWebapp.java    From helix with Apache License 2.0 5 votes vote down vote up
void verifyAddCluster() throws IOException, InterruptedException {
  String httpUrlBase = "http://localhost:" + ADMIN_PORT + "/clusters";
  Map<String, String> paraMap = new HashMap<String, String>();

  paraMap.put(JsonParameters.CLUSTER_NAME, clusterName);
  paraMap.put(JsonParameters.MANAGEMENT_COMMAND, ClusterSetup.addCluster);

  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 zn = mapper.readValue(new StringReader(sw.toString()), ZNRecord.class);
  AssertJUnit.assertTrue(zn.getListField("clusters").contains(clusterName));

}
 
Example 8
Source File: TestClusterManagementWebapp.java    From helix with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 9
Source File: TestHelixAdminScenariosRest.java    From helix with Apache License 2.0 4 votes vote down vote up
static String assertSuccessPostOperation(String url, Map<String, String> jsonParameters,
    Map<String, String> extraForm, boolean hasException) throws IOException {
  Reference resourceRef = new Reference(url);

  int numRetries = 0;
  while (numRetries <= MAX_RETRIES) {
    Request request = new Request(Method.POST, resourceRef);

    if (extraForm != null) {
      String entity =
          JsonParameters.JSON_PARAMETERS + "="
              + ClusterRepresentationUtil.ObjectToJson(jsonParameters);
      for (String key : extraForm.keySet()) {
        entity = entity + "&" + (key + "=" + extraForm.get(key));
      }
      request.setEntity(entity, MediaType.APPLICATION_ALL);
    } else {
      request
          .setEntity(
              JsonParameters.JSON_PARAMETERS + "="
                  + ClusterRepresentationUtil.ObjectToJson(jsonParameters),
              MediaType.APPLICATION_ALL);
    }

    Response response = _gClient.handle(request);
    Representation result = response.getEntity();
    StringWriter sw = new StringWriter();

    if (result != null) {
      result.write(sw);
    }

    int code = response.getStatus().getCode();
    boolean successCode =
        code == Status.SUCCESS_NO_CONTENT.getCode() || code == Status.SUCCESS_OK.getCode();
    if (successCode || numRetries == MAX_RETRIES) {
      Assert.assertTrue(successCode);
      Assert.assertTrue(hasException == sw.toString().toLowerCase().contains("exception"));
      return sw.toString();
    }
    numRetries++;
  }
  Assert.fail("Request failed after all retries");
  return null;
}