Java Code Examples for org.restlet.Request

The following examples show how to use org.restlet.Request. These examples are extracted from open source projects. 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 Project: scava   Source File: TestProjectImportResource.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@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());
}
 
Example 2
@Override
public T create( Class<T> entityType, Request request, Response response, Context context )
{
    final Map<String, Object> attributes = request.getAttributes();
    String id = (String) attributes.get( "id" );

    ValueBuilder<T> builder = vbf.newValueBuilderWithState(
        resourceType,
        descriptor -> findValue( attributes, descriptor ),
        descriptor -> null,
        descriptor -> null,
        descriptor -> null
    );
    //noinspection unchecked
    ServerResource.Parameters<T> params = builder.prototypeFor( ServerResource.Parameters.class );
    params.id().set( id );
    params.entityType().set( entityType );
    params.context().set( this.context );
    params.request().set( request );
    params.router().set( router );
    params.response().set( response );
    return builder.newInstance();
}
 
Example 3
Source Project: FoxBPM   Source File: FoxbpmStatusService.java    License: Apache License 2.0 6 votes vote down vote up
public Status getStatus(Throwable throwable, Request request, Response response) {
	Status status = null;
	if (throwable instanceof JsonMappingException && throwable.getCause() != null) {
		status = getSpecificStatus(throwable.getCause(), request, response);
	}
	if (status == null) {
		Throwable causeThrowable = null;
		if (throwable.getCause() != null && throwable.getCause() instanceof FoxBPMException) {
			causeThrowable = throwable.getCause();
		} else {
			causeThrowable = throwable;
		}
		status = getSpecificStatus(causeThrowable, request, response);
	}
	return status != null ? status : Status.SERVER_ERROR_INTERNAL;
}
 
Example 4
@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));
}
 
Example 5
Source Project: helix   Source File: AdminTestHelper.java    License: 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 6
@Test
public void testGet() {
  EasyMock.reset(_helixMirrorMakerManager);
  TopicPartition tp = new TopicPartition("test", 3);
  Set<TopicPartition> blacklist = new HashSet<>();
  blacklist.add(tp);
  EasyMock.expect(_helixMirrorMakerManager.getTopicPartitionBlacklist()).andReturn(null).andReturn(blacklist);
  EasyMock.replay(_helixMirrorMakerManager);

  Request request = ControllerRequestURLBuilder.baseUrl(REQUEST_URL)
      .getBlacklistRequestUrl();
  Response response = RestTestBase.HTTP_CLIENT.handle(request);
  Assert.assertEquals(response.getStatus(), Status.SUCCESS_OK);
  Assert.assertEquals(response.getEntityAsText(), "{}");

  response = RestTestBase.HTTP_CLIENT.handle(request);
  Assert.assertEquals(response.getStatus(), Status.SUCCESS_OK);
  Assert.assertEquals(response.getEntityAsText(), "{\"blacklist\":[{\"partition\":3,\"topic\":\"test\"}]}");
}
 
Example 7
Source Project: attic-polygene-java   Source File: FormRequestWriter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean writeRequest(Object requestObject, Request request) throws ResourceException
{
   if (requestObject instanceof Form)
   {
      // Form as query parameters
      if (request.getMethod().equals(Method.GET))
         request.getResourceRef().setQuery(((Form)requestObject).getQueryString());
      else
         request.setEntity(((Form)requestObject).getWebRepresentation(CharacterSet.UTF_8));

      return true;
   }

   return false;
}
 
Example 8
Source Project: helix   Source File: AdminTestHelper.java    License: Apache License 2.0 6 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 9
Source Project: floodlight_with_topoguard   Source File: RestApiServer.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Restlet createInboundRoot() {
    Router baseRouter = new Router(context);
    baseRouter.setDefaultMatchingMode(Template.MODE_STARTS_WITH);
    for (RestletRoutable rr : restlets) {
        baseRouter.attach(rr.basePath(), rr.getRestlet(context));
    }

    Filter slashFilter = new Filter() {            
        @Override
        protected int beforeHandle(Request request, Response response) {
            Reference ref = request.getResourceRef();
            String originalPath = ref.getPath();
            if (originalPath.contains("//"))
            {
                String newPath = originalPath.replaceAll("/+", "/");
                ref.setPath(newPath);
            }
            return Filter.CONTINUE;
        }

    };
    slashFilter.setNext(baseRouter);
    
    return slashFilter;
}
 
Example 10
public void writeRequest( Request request, Object queryRequest )
{
    if( !requestWriterDelegator.writeRequest( queryRequest, request ) )
    {
        throw new IllegalArgumentException( "Illegal query request type:" + queryRequest.getClass().getName() );
    }
}
 
Example 11
Source Project: attic-polygene-java   Source File: PolygeneEntityRestlet.java    License: Apache License 2.0 5 votes vote down vote up
private <K> K convertToObject( Class<K> type, Request request )
{
    try
    {
        return converter.toObject( request.getEntity(), type, null );
    }
    catch( IOException e )
    {
        throw new ConversionException( request.getEntityAsText() );
    }
}
 
Example 12
Source Project: attic-polygene-java   Source File: PolygeneEntityRestlet.java    License: Apache License 2.0 5 votes vote down vote up
private void delete( Request request, Response response )
{
    execute( request, response, resource -> {
        resource.delete();
        response.setStatus( Status.SUCCESS_NO_CONTENT );
    } );
}
 
Example 13
void updateQueryRequest( Request request )
{
    request.setClientInfo( info );

    // Update cache information
    if( cache != null )
    {
        cache.updateQueryConditions( request );
    }

}
 
Example 14
Source Project: scava   Source File: ApiHelper.java    License: Eclipse Public License 2.0 5 votes vote down vote up
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);
}
 
Example 15
Source Project: scava   Source File: TestProjectResource.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testGet() {
	Client client = new Client(Protocol.HTTP);
	Request request = new Request(Method.GET, "http://localhost:8182/projects/p/ant");
	Response response = client.handle(request);
	
	validateResponse(response, 200);
	
	// TODO: check the JSON
}
 
Example 16
Source Project: helix   Source File: TestClusterManagementWebapp.java    License: 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 17
Source Project: uReplicator   Source File: ManagerRequestURLBuilder.java    License: Apache License 2.0 5 votes vote down vote up
public Request postInstanceRebalance() {
  String requestUrl = StringUtils.join(new String[]{
      _baseUrl, "/admin/force_rebalance"
  });

  Request request = new Request(Method.POST, requestUrl);
  return request;
}
 
Example 18
Source Project: uReplicator   Source File: ManagerRequestURLBuilder.java    License: Apache License 2.0 5 votes vote down vote up
public Request getTopicExternalViewRequestUrl(String topic) {
  String requestUrl = StringUtils.join(new String[]{
      _baseUrl, "/topics/", topic
  });

  Request request = new Request(Method.GET, requestUrl);
  return request;
}
 
Example 19
Source Project: uReplicator   Source File: ManagerRequestURLBuilder.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 20
Source Project: uReplicator   Source File: ManagerRequestURLBuilder.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 21
Source Project: uReplicator   Source File: ManagerRequestURLBuilder.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 22
Source Project: uReplicator   Source File: ManagerRequestURLBuilder.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 23
Source Project: uReplicator   Source File: TestAdminManagement.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testPostRebalance() {
  Request request = ManagerRequestURLBuilder.baseUrl(REQUEST_URL).postInstanceRebalance();
  Response response = HTTP_CLIENT.handle(request);
  String responseString = response.getEntityAsText();
  Assert.assertEquals(response.getStatus(), Status.SUCCESS_OK);
  Assert.assertEquals(responseString, "{\"opt\":\"force_rebalance\",\"status\":200}");
}
 
Example 24
Source Project: uReplicator   Source File: TestAdminManagement.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testPostControllerRebalance() {
  Request request = ManagerRequestURLBuilder.baseUrl(REQUEST_URL).postSetControllerRebalance(true);
  Response response = HTTP_CLIENT.handle(request);
  String responseString = response.getEntityAsText();
  Assert.assertEquals(response.getStatus(), Status.SUCCESS_OK);
  Assert.assertEquals(responseString, "{\"execution\":{},\"managerAutoscaling\":true,\"status\":{}}");
  request = ManagerRequestURLBuilder.baseUrl(REQUEST_URL).postSetControllerRebalance("sjc1a", "sjc1-agg1", true);
  response = HTTP_CLIENT.handle(request);
  responseString = response.getEntityAsText();
  Assert.assertEquals(response.getStatus(), Status.SUCCESS_OK);
  Assert.assertEquals(responseString, "{\"execution\":{},\"managerAutoscaling\":true,\"status\":{}}");
}
 
Example 25
Source Project: uReplicator   Source File: TestAdminManagement.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGetControllerRebalance() {
  Request request = ManagerRequestURLBuilder.baseUrl(REQUEST_URL).getControllerRebalanceStatus();
  Response response = HTTP_CLIENT.handle(request);
  String responseString = response.getEntityAsText();
  Assert.assertEquals(response.getStatus(), Status.SUCCESS_OK);
  Assert.assertEquals(responseString, "{}");
}
 
Example 26
Source Project: attic-polygene-java   Source File: PolygeneEntityRestlet.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void handle( Request request, Response response )
{
    try
    {
        super.handle( request, response );
        Method method = request.getMethod();
        if( method.equals( Method.GET ) )
        {
            get( request, response );
        }
        if( method.equals( Method.DELETE ) )
        {
            delete( request, response );
        }
        if( method.equals( Method.POST ) )
        {
            post( request, response );
        }
        if( method.equals( Method.PUT ) )
        {
            put( request, response );
        }
    }
    catch( RuntimeException e )
    {
        e.printStackTrace();
        throw e;
    }
}
 
Example 27
Source Project: uReplicator   Source File: ControllerRequestURLBuilder.java    License: Apache License 2.0 5 votes vote down vote up
public Request getTopicExternalViewRequestUrl(String topic) {
  String requestUrl = StringUtils.join(new String[]{
      _baseUrl, "/topics/", topic
  });

  Request request = new Request(Method.GET, requestUrl);
  return request;
}
 
Example 28
Source Project: attic-polygene-java   Source File: ClientCache.java    License: Apache License 2.0 5 votes vote down vote up
public void updateQueryConditions( Request request )
    {
        String identity = pathToIdentity.get( getIdentityPath( request.getResourceRef() ) );
        if( identity != null )
        {
            CacheInfo cacheInfo = identityToTimestamp.get( identity );
            if( cacheInfo != null )
            {
//            LoggerFactory.getLogger( ClientCache.class ).info( "Send:  "+cacheInfo.getEntity()+" ("+request.getMethod().getName()+":"+request.getResourceRef()+") -> "+cacheInfo.getLastModified() );
                request.getConditions().setModifiedSince( from( cacheInfo.getLastModified() ) );
            }
        }
    }
 
Example 29
Source Project: uReplicator   Source File: ControllerRequestURLBuilder.java    License: Apache License 2.0 5 votes vote down vote up
public Request getNoProgressRequestUrl() {
  String requestUrl = StringUtils.join(new String[]{
      _baseUrl, "/noprogress"
  });
  Request request = new Request(Method.GET, requestUrl);
  return request;
}
 
Example 30
Source Project: uReplicator   Source File: ControllerRequestURLBuilder.java    License: Apache License 2.0 5 votes vote down vote up
public Request getBlacklistRequestUrl() {
  String requestUrl = StringUtils.join(new String[]{
      _baseUrl, "/blacklist"
  });
  Request request = new Request(Method.GET, requestUrl);
  return request;
}