org.restlet.Client Java Examples

The following examples show how to use org.restlet.Client. 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: XEStudentEnrollment.java    From unitime with Apache License 2.0 6 votes vote down vote up
public XEStudentEnrollment() {
	List<Protocol> protocols = new ArrayList<Protocol>();
	protocols.add(Protocol.HTTP);
	protocols.add(Protocol.HTTPS);
	iClient = new Client(protocols);
	try {
		String clazz = ApplicationProperty.CustomizationExternalTerm.value();
		if (clazz == null || clazz.isEmpty())
			iExternalTermProvider = new BannerTermProvider();
		else
			iExternalTermProvider = (ExternalTermProvider)Class.forName(clazz).getConstructor().newInstance();
	} catch (Exception e) {
		sLog.error("Failed to create external term provider, using the default one instead.", e);
		iExternalTermProvider = new BannerTermProvider();
	}
}
 
Example #3
Source File: AdminTestBase.java    From helix with Apache License 2.0 6 votes vote down vote up
@BeforeSuite
public void beforeSuite() throws Exception {
  // TODO: use logging.properties file to config java.util.logging.Logger levels
  java.util.logging.Logger topJavaLogger = java.util.logging.Logger.getLogger("");
  topJavaLogger.setLevel(Level.WARNING);

  // start zk
  _zkServer = TestHelper.startZkServer(ZK_ADDR);
  AssertJUnit.assertTrue(_zkServer != null);
  ZKClientPool.reset();

  _gZkClient =
      new ZkClient(ZK_ADDR, ZkClient.DEFAULT_SESSION_TIMEOUT, ZkClient.DEFAULT_CONNECTION_TIMEOUT,
          new ZNRecordSerializer());
  _gSetupTool = new ClusterSetup(_gZkClient);

  // start admin
  _adminThread = new AdminThread(ZK_ADDR, ADMIN_PORT);
  _adminThread.start();

  // create a client
  _gClient = new Client(Protocol.HTTP);

  // wait for the web service to start
  Thread.sleep(100);
}
 
Example #4
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 #5
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 #6
Source File: DegreeWorksPlanScraper.java    From unitime with Apache License 2.0 6 votes vote down vote up
public DegreeWorksPlanScraper(Set<String> filter) {
	List<Protocol> protocols = new ArrayList<Protocol>();
	protocols.add(Protocol.HTTP);
	protocols.add(Protocol.HTTPS);
	iClient = new Client(protocols);
	iFilter = filter;
	try {
		String clazz = ApplicationProperty.CustomizationExternalTerm.value();
		if (clazz == null || clazz.isEmpty())
			iExternalTermProvider = new BannerTermProvider();
		else
			iExternalTermProvider = (ExternalTermProvider)Class.forName(clazz).getConstructor().newInstance();
	} catch (Exception e) {
		sLog.error("Failed to create external term provider, using the default one instead.", e);
		iExternalTermProvider = new BannerTermProvider();
	}
}
 
Example #7
Source File: DegreeWorksCourseRequests.java    From unitime with Apache License 2.0 6 votes vote down vote up
public DegreeWorksCourseRequests() {
	List<Protocol> protocols = new ArrayList<Protocol>();
	protocols.add(Protocol.HTTP);
	protocols.add(Protocol.HTTPS);
	iClient = new Client(protocols);
	try {
		String clazz = ApplicationProperty.CustomizationExternalTerm.value();
		if (clazz == null || clazz.isEmpty())
			iExternalTermProvider = new BannerTermProvider();
		else
			iExternalTermProvider = (ExternalTermProvider)Class.forName(clazz).getConstructor().newInstance();
	} catch (Exception e) {
		sLog.error("Failed to create external term provider, using the default one instead.", e);
		iExternalTermProvider = new BannerTermProvider();
	}
}
 
Example #8
Source File: PurdueCourseRequestsValidationProvider.java    From unitime with Apache License 2.0 6 votes vote down vote up
public PurdueCourseRequestsValidationProvider() {
	List<Protocol> protocols = new ArrayList<Protocol>();
	protocols.add(Protocol.HTTP);
	protocols.add(Protocol.HTTPS);
	iClient = new Client(protocols);
	Context cx = new Context();
	cx.getParameters().add("readTimeout", getSpecialRegistrationApiReadTimeout());
	try {
		String clazz = ApplicationProperty.CustomizationExternalTerm.value();
		if (clazz == null || clazz.isEmpty())
			iExternalTermProvider = new BannerTermProvider();
		else
			iExternalTermProvider = (ExternalTermProvider)Class.forName(clazz).getConstructor().newInstance();
	} catch (Exception e) {
		sLog.error("Failed to create external term provider, using the default one instead.", e);
		iExternalTermProvider = new BannerTermProvider();
	}
}
 
Example #9
Source File: SimplifiedCourseRequestsValidationProvider.java    From unitime with Apache License 2.0 6 votes vote down vote up
public SimplifiedCourseRequestsValidationProvider() {
	List<Protocol> protocols = new ArrayList<Protocol>();
	protocols.add(Protocol.HTTP);
	protocols.add(Protocol.HTTPS);
	iClient = new Client(protocols);
	try {
		String clazz = ApplicationProperty.CustomizationExternalTerm.value();
		if (clazz == null || clazz.isEmpty())
			iExternalTermProvider = new BannerTermProvider();
		else
			iExternalTermProvider = (ExternalTermProvider)Class.forName(clazz).getConstructor().newInstance();
	} catch (Exception e) {
		sLog.error("Failed to create external term provider, using the default one instead.", e);
		iExternalTermProvider = new BannerTermProvider();
	}
}
 
Example #10
Source File: RemoteCarService.java    From microservices-comparison with Apache License 2.0 6 votes vote down vote up
@Override
public List<Car> list() {
    Client client = new Client(new Context(), Protocol.HTTPS);
    Series<Parameter> parameters = client.getContext().getParameters();
    parameters.add("truststorePath", System.getProperty("javax.net.ssl.trustStore"));

    ClientResource clientResource = new ClientResource("https://localhost:8043/api/cars/cars");
    clientResource.setNext(client);
    ChallengeResponse challenge = new ChallengeResponse(ChallengeScheme.HTTP_OAUTH_BEARER);
    challenge.setRawValue(Request.getCurrent().getAttributes().getOrDefault("token", "").toString());
    clientResource.setChallengeResponse(challenge);
    CarServiceInterface carServiceInterface = clientResource.wrap(CarServiceInterface.class);
    Car[] allCars = carServiceInterface.getAllCars();
    try {
        client.stop();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return asList(allCars);
}
 
Example #11
Source File: TestProjectImportResource.java    From scava with 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 #12
Source File: TestProjectImportResource.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
@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());
}
 
Example #13
Source File: StaticWebRoutable.java    From floodlight_with_topoguard with Apache License 2.0 5 votes vote down vote up
@Override
public Restlet getRestlet(Context context) {
       Router router = new Router(context);
       router.attach("", new Directory(context, "clap://classloader/web/"));
       context.setClientDispatcher(new Client(context, Protocol.CLAP));
       return router;
}
 
Example #14
Source File: ClientList.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
@Override
    public boolean add(Client client) {
        // Set the client's context, if the client does not have already one.
//        if (client.getContext() == null) {
//            client.setContext(getContext().createChildContext());
//        }

        return super.add(client);
    }
 
Example #15
Source File: GeocodingBackend.java    From unitime with Apache License 2.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
	List<Protocol> protocols = new ArrayList<Protocol>();
	protocols.add(Protocol.HTTP);
	protocols.add(Protocol.HTTPS);
	iClient = new Client(protocols);
}
 
Example #16
Source File: TestProjectResource.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
@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();
}
 
Example #17
Source File: TestProjectResource.java    From scava with 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 #18
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 #19
Source File: ApiHelper.java    From scava with Eclipse Public License 2.0 4 votes vote down vote up
public ApiHelper() {
	client = new Client(Protocol.HTTP);
}
 
Example #20
Source File: AdminTestHelper.java    From helix with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example #21
Source File: ClientList.java    From DeviceConnect-Android with MIT License 3 votes vote down vote up
/**
     * Adds a new client connector in the map supporting the given protocol.
     * 
     * @param protocol
     *            The connector protocol.
     * @return The added client.
     */
    public Client add(Protocol protocol) {
        final Client result = new Client(protocol);
//        result.setContext(getContext().createChildContext());
        add(result);
        return result;
    }
 
Example #22
Source File: ClientList.java    From DeviceConnect-Android with MIT License 2 votes vote down vote up
/**
 * Constructor.
 * 
 * @param context
 *            The context.
 */
public ClientList(Context context) {
    super(new CopyOnWriteArrayList<Client>());
    this.context = context;
}