Search in sources :

Example 46 with Client

use of com.sun.jersey.api.client.Client in project apex-core by apache.

the class StramMiniClusterTest method testWebService.

/**
   * Verify the web service deployment and lifecycle functionality
   *
   * @throws Exception
   */
//disabled due to web service init delay issue
@Ignore
@Test
public void testWebService() throws Exception {
    // single container topology of inline input and module
    Properties props = new Properties();
    props.put(StreamingApplication.APEX_PREFIX + "stream.input.classname", TestGeneratorInputOperator.class.getName());
    props.put(StreamingApplication.APEX_PREFIX + "stream.input.outputNode", "module1");
    props.put(StreamingApplication.APEX_PREFIX + "module.module1.classname", GenericTestOperator.class.getName());
    LOG.info("Initializing Client");
    LogicalPlanConfiguration tb = new LogicalPlanConfiguration(new Configuration(false));
    tb.addFromProperties(props, null);
    StramClient client = new StramClient(new Configuration(yarnCluster.getConfig()), createDAG(tb));
    if (StringUtils.isBlank(System.getenv("JAVA_HOME"))) {
        // JAVA_HOME not set in the yarn mini cluster
        client.javaCmd = "java";
    }
    try {
        client.start();
        client.startApplication();
        // attempt web service connection
        ApplicationReport appReport = client.getApplicationReport();
        // delay to give web service time to fully initialize
        Thread.sleep(5000);
        Client wsClient = Client.create();
        wsClient.setFollowRedirects(true);
        WebResource r = wsClient.resource("http://" + appReport.getTrackingUrl()).path(StramWebServices.PATH).path(StramWebServices.PATH_INFO);
        LOG.info("Requesting: " + r.getURI());
        ClientResponse response = r.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
        assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
        JSONObject json = response.getEntity(JSONObject.class);
        LOG.info("Got response: " + json.toString());
        assertEquals("incorrect number of elements", 1, json.length());
        assertEquals("appId", appReport.getApplicationId().toString(), json.get("id"));
        r = wsClient.resource("http://" + appReport.getTrackingUrl()).path(StramWebServices.PATH).path(StramWebServices.PATH_PHYSICAL_PLAN_OPERATORS);
        LOG.info("Requesting: " + r.getURI());
        response = r.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
        assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
        json = response.getEntity(JSONObject.class);
        LOG.info("Got response: " + json.toString());
    } finally {
        //LOG.info("waiting...");
        //synchronized (this) {
        //  this.wait();
        //}
        //boolean result = client.monitorApplication();
        client.killApplication();
        client.stop();
    }
}
Also used : LogicalPlanConfiguration(com.datatorrent.stram.plan.logical.LogicalPlanConfiguration) ApplicationReport(org.apache.hadoop.yarn.api.records.ApplicationReport) ClientResponse(com.sun.jersey.api.client.ClientResponse) Configuration(org.apache.hadoop.conf.Configuration) LogicalPlanConfiguration(com.datatorrent.stram.plan.logical.LogicalPlanConfiguration) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) JSONObject(org.codehaus.jettison.json.JSONObject) GenericTestOperator(com.datatorrent.stram.engine.GenericTestOperator) WebResource(com.sun.jersey.api.client.WebResource) TestGeneratorInputOperator(com.datatorrent.stram.engine.TestGeneratorInputOperator) Properties(java.util.Properties) AMRMClient(org.apache.hadoop.yarn.client.api.AMRMClient) Client(com.sun.jersey.api.client.Client) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 47 with Client

use of com.sun.jersey.api.client.Client in project apex-core by apache.

the class WebServicesVersionConversionTest method testVersioning.

@Test
public void testVersioning() throws Exception {
    WebServicesClient wsClient = new WebServicesClient();
    Client client = wsClient.getClient();
    WebResource ws = client.resource("http://localhost:" + port).path("/new_path");
    WebServicesVersionConversion.Converter versionConverter = new WebServicesVersionConversion.Converter() {

        @Override
        public String convertCommandPath(String path) {
            if (path.equals("/new_path")) {
                return "/old_path";
            }
            return path;
        }

        @Override
        public String convertResponse(String path, String response) {
            if (path.equals("/new_path")) {
                try {
                    JSONObject json = new JSONObject(response);
                    json.put("new_key", json.get("old_key"));
                    json.remove("old_key");
                    return json.toString();
                } catch (JSONException ex) {
                    throw new RuntimeException(ex);
                }
            }
            return response;
        }
    };
    VersionConversionFilter versionConversionFilter = new VersionConversionFilter(versionConverter);
    client.addFilter(versionConversionFilter);
    JSONObject result = new JSONObject(ws.get(String.class));
    Assert.assertEquals(result.getString("url"), "/old_path");
    Assert.assertEquals(result.getString("new_key"), "value");
    Assert.assertEquals(result.getString("other_key"), "other_value");
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) WebResource(com.sun.jersey.api.client.WebResource) JSONException(org.codehaus.jettison.json.JSONException) WebServicesClient(com.datatorrent.stram.util.WebServicesClient) WebServicesClient(com.datatorrent.stram.util.WebServicesClient) Client(com.sun.jersey.api.client.Client) VersionConversionFilter(com.datatorrent.stram.client.WebServicesVersionConversion.VersionConversionFilter) Test(org.junit.Test)

Example 48 with Client

use of com.sun.jersey.api.client.Client in project gfm_viewer by satyagraha.

the class WebServiceClientDefault method getClient.

private Client getClient(String endpoint) {
    // set up standard properties
    DefaultApacheHttpClient4Config clientConfig = new DefaultApacheHttpClient4Config();
    Map<String, Object> clientProperties = clientConfig.getProperties();
    clientProperties.put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true);
    clientConfig.getClasses().addAll(ConnUtilities.getJerseyProviders());
    // see if proxy needed
    URI uri;
    try {
        uri = new URI(endpoint);
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
    WebProxyData webProxyData = webProxyConfig.getWebProxyData(uri);
    if (webProxyData != null) {
        if (webProxyData.getProxyUri() != null) {
            clientProperties.put(DefaultApacheHttpClient4Config.PROPERTY_PROXY_URI, webProxyData.getProxyUri());
            if (webProxyData.getUserId() != null) {
                clientProperties.put(DefaultApacheHttpClient4Config.PROPERTY_PROXY_USERNAME, webProxyData.getUserId());
                if (webProxyData.getPassword() != null) {
                    clientProperties.put(DefaultApacheHttpClient4Config.PROPERTY_PROXY_PASSWORD, webProxyData.getPassword());
                }
            }
        }
    }
    // set up client properties
    clientProperties.put(DefaultApacheHttpClient4Config.PROPERTY_CONNECTION_MANAGER, connectionManager);
    LOGGER.fine("clientProperties(): " + clientProperties);
    // build client
    Client client = ApacheHttpClient4.create(clientConfig);
    return client;
}
Also used : DefaultApacheHttpClient4Config(com.sun.jersey.client.apache4.config.DefaultApacheHttpClient4Config) WebProxyData(code.satyagraha.gfm.support.api.WebProxyConfig.WebProxyData) URISyntaxException(java.net.URISyntaxException) Client(com.sun.jersey.api.client.Client) WebServiceClient(code.satyagraha.gfm.support.api.WebServiceClient) URI(java.net.URI)

Example 49 with Client

use of com.sun.jersey.api.client.Client in project ORCID-Source by ORCID.

the class MailGunManager method sendEmail.

public boolean sendEmail(String from, String to, String subject, String text, String html, boolean custom) {
    Client client = Client.create();
    client.addFilter(new HTTPBasicAuthFilter("api", getApiKey()));
    // determine correct api based off domain.
    WebResource webResource = null;
    String toAddress = to.trim();
    String fromEmail = getFromEmail(from);
    if (shouldBeSentThroughDedicatedIP(toAddress)) {
        if (custom)
            webResource = client.resource(getAltNotifyApiUrl());
        else if (fromEmail.endsWith("@verify.orcid.org"))
            webResource = client.resource(getAltVerifyApiUrl());
        else if (fromEmail.endsWith("@notify.orcid.org"))
            webResource = client.resource(getAltNotifyApiUrl());
        else
            webResource = client.resource(getAltApiUrl());
    } else {
        if (custom)
            webResource = client.resource(getNotifyApiUrl());
        else if (fromEmail.endsWith("@verify.orcid.org"))
            webResource = client.resource(getVerifyApiUrl());
        else if (fromEmail.endsWith("@notify.orcid.org"))
            webResource = client.resource(getNotifyApiUrl());
        else
            webResource = client.resource(getApiUrl());
    }
    MultivaluedMapImpl formData = new MultivaluedMapImpl();
    formData.add("from", from);
    formData.add("to", to);
    formData.add("subject", subject);
    formData.add("text", text);
    if (html != null) {
        formData.add("html", html);
    }
    formData.add("o:testmode", testmode);
    if (testmode.equals("yes"))
        LOGGER.error("Email form data: \n" + formData.toString());
    // sandbox
    if (to.matches(filter)) {
        ClientResponse cr = webResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, formData);
        if (cr.getStatus() != 200) {
            LOGGER.error("Post MailGunManager.sendEmail not accepted: " + formData.toString());
            return false;
        }
    }
    return true;
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) WebResource(com.sun.jersey.api.client.WebResource) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) Client(com.sun.jersey.api.client.Client) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)

Example 50 with Client

use of com.sun.jersey.api.client.Client in project ORCID-Source by ORCID.

the class OpenIDConnectTest method check403UserInfoWithoutToken.

@Test
public void check403UserInfoWithoutToken() throws JSONException {
    //get userinfo
    Client client = Client.create();
    WebResource webResource = client.resource(baseUri + "/oauth/userinfo");
    ClientResponse userInfo = webResource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
    Assert.assertEquals(403, userInfo.getStatus());
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) WebResource(com.sun.jersey.api.client.WebResource) Client(com.sun.jersey.api.client.Client) Test(org.junit.Test)

Aggregations

Client (com.sun.jersey.api.client.Client)87 ClientResponse (com.sun.jersey.api.client.ClientResponse)61 Test (org.junit.Test)59 URI (java.net.URI)51 TimelineEntity (org.apache.hadoop.yarn.api.records.timelineservice.TimelineEntity)36 Set (java.util.Set)30 WebResource (com.sun.jersey.api.client.WebResource)19 HashSet (java.util.HashSet)19 GenericType (com.sun.jersey.api.client.GenericType)17 DefaultClientConfig (com.sun.jersey.api.client.config.DefaultClientConfig)9 TimelineMetric (org.apache.hadoop.yarn.api.records.timelineservice.TimelineMetric)9 JSONObject (org.codehaus.jettison.json.JSONObject)7 ClientConfig (com.sun.jersey.api.client.config.ClientConfig)6 URLConnectionClientHandler (com.sun.jersey.client.urlconnection.URLConnectionClientHandler)4 ArrayList (java.util.ArrayList)4 FlowRunEntity (org.apache.hadoop.yarn.api.records.timelineservice.FlowRunEntity)4 YarnClient (org.apache.hadoop.yarn.client.api.YarnClient)4 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)3 HTTPBasicAuthFilter (com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)3 JSONException (org.codehaus.jettison.json.JSONException)3