Search in sources :

Example 51 with DefaultClientConfig

use of com.sun.jersey.api.client.config.DefaultClientConfig in project onebusaway-application-modules by camsys.

the class RemoteConnectionServiceImpl method postBinaryData.

@Override
public <T> T postBinaryData(String url, File data, Class<T> responseType) {
    T response = null;
    ClientConfig config = new DefaultClientConfig();
    Client client = Client.create(config);
    WebResource resource = client.resource(url);
    try {
        response = resource.accept("text/csv").type("text/csv").post(responseType, new FileInputStream(data));
    } catch (UniformInterfaceException e) {
        log.error("Unable to read response from the server.");
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        log.error("CSV File not found. It is not uploaded correctly");
        e.printStackTrace();
    }
    return response;
}
Also used : DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) FileNotFoundException(java.io.FileNotFoundException) WebResource(com.sun.jersey.api.client.WebResource) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) Client(com.sun.jersey.api.client.Client) FileInputStream(java.io.FileInputStream)

Example 52 with DefaultClientConfig

use of com.sun.jersey.api.client.config.DefaultClientConfig in project lobcder by skoulouzis.

the class TestREST method testTicketTranslator.

// @Test
// public void testReservation() throws IOException {
// System.err.println("testReservation");
// try {
// createCollection();
// //Wait for replication
// Thread.sleep(15000);
// 
// 
// // /rest/reservation/get_workers/?id=all
// WebResource webResource = restClient.resource(restURL);
// 
// //Get list of workers
// MultivaluedMap<String, String> params = new MultivaluedMapImpl();
// params.add("id", "all");
// WebResource res = webResource.path("reservation").path("get_workers").queryParams(params);
// List<WorkerStatus> workersList = res.accept(MediaType.APPLICATION_XML).
// get(new GenericType<List<WorkerStatus>>() {
// });
// 
// 
// //If we have workers ask for a path reservation
// if (workersList != null && workersList.size() > 0) {
// //rest/reservation/5455/request/?dataPath=/&storageSiteHost=sps1&storageSiteHost=sps2&storageSiteHost=sps3
// params = new MultivaluedMapImpl();
// String dataPath = "file1";
// params.add("dataName", dataPath);
// for (WorkerStatus w : workersList) {
// params.add("storageSiteHost", w.hostName);
// }
// 
// res = webResource.path("reservation").path("some_communication_id").path("request").queryParams(params);
// ReservationInfo info = res.accept(MediaType.APPLICATION_XML).
// get(new GenericType<ReservationInfo>() {
// });
// 
// assertNotNull(info);
// assertNotNull(info.communicationID);
// assertNotNull(info.storageHost);
// assertNotNull(info.storageHostIndex);
// assertNotNull(info.workerDataAccessURL);
// 
// 
// //Check if worker is ready
// params = new MultivaluedMapImpl();
// params.add("host", info.storageHost);
// 
// 
// res = webResource.path("reservation").path("workers").queryParams(params);
// List<WorkerStatus> list = res.accept(MediaType.APPLICATION_XML).
// get(new GenericType<List<WorkerStatus>>() {
// });
// 
// assertNotNull(list);
// assertFalse(list.isEmpty());
// for (WorkerStatus w : list) {
// assertNotNull(w.status);
// assertNotNull(w.hostName);
// assertEquals("READY", w.status);
// }
// 
// //Now get the file
// GetMethod get = new GetMethod(info.workerDataAccessURL);
// int status = client.executeMethod(get);
// assertEquals(HttpStatus.SC_OK, status);
// assertEquals("foo", get.getResponseBodyAsString());
// 
// 
// 
// 
// //run without host names
// params = new MultivaluedMapImpl();
// dataPath = "file1";
// params.add("dataName", dataPath);
// res = webResource.path("reservation").path("some_communication_id").path("request").queryParams(params);
// info = res.accept(MediaType.APPLICATION_XML).
// get(new GenericType<ReservationInfo>() {
// });
// 
// assertNotNull(info);
// assertNotNull(info.communicationID);
// assertNotNull(info.storageHostIndex);
// assertNotNull(info.workerDataAccessURL);
// 
// 
// //Now get the file
// get = new GetMethod(info.workerDataAccessURL);
// status = client.executeMethod(get);
// assertEquals(HttpStatus.SC_OK, status);
// assertEquals("foo", get.getResponseBodyAsString());
// 
// }
// } catch (Exception ex) {
// Logger.getLogger(TestREST.class.getName()).log(Level.SEVERE, null, ex);
// } finally {
// utils.deleteResource(testcol, false);
// }
// }
// @Test
// public void testGetWorkersStatus() throws IOException {
// System.err.println("testGetWorkersStatus");
// try {
// createCollection();
// WebResource webResource = restClient.resource(restURL);
// //        rest/reservation/workers/?host=kscvdfv&host=sp2&host=192.168.1.1
// MultivaluedMap<String, String> params = new MultivaluedMapImpl();
// params.add("host", "host1");
// params.add("host", "host2");
// params.add("host", "host3");
// 
// WebResource res = webResource.path("reservation").path("workers").queryParams(params);
// List<WorkerStatus> list = res.accept(MediaType.APPLICATION_XML).
// get(new GenericType<List<WorkerStatus>>() {
// });
// 
// assertNotNull(list);
// assertFalse(list.isEmpty());
// for (WorkerStatus w : list) {
// assertNotNull(w.status);
// assertNotNull(w.hostName);
// }
// 
// 
// } finally {
// utils.deleteResource(testcol, false);
// }
// }
@Test
public void testTicketTranslator() throws IOException {
    System.err.println("testTicketTranslator");
    String testcol = root + "testResourceForTicketTranslator/";
    String testURI1 = testcol + "file1";
    try {
        utils.createCollection(testcol, true);
        utils.createFile(this.root + testURI1, true);
        ClientConfig clientConfig = new DefaultClientConfig();
        clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
        Client nonAuthRestClient = Client.create(clientConfig);
        WebResource webResource = nonAuthRestClient.resource(translatorURL);
        WebResource res = webResource.path("getshort").path(password);
        String shortToken = res.accept(MediaType.TEXT_PLAIN).get(String.class);
        assertNotNull(shortToken);
        Client shortAuthRestClient = Client.create(clientConfig);
        shortAuthRestClient.addFilter(new com.sun.jersey.api.client.filter.HTTPBasicAuthFilter(username, shortToken));
        webResource = shortAuthRestClient.resource(restURL);
        MultivaluedMap<String, String> params = new MultivaluedMapImpl();
        params.add("path", "/testResourceForTicketTranslator");
        res = webResource.path("items").path("query").queryParams(params);
        List<LogicalDataWrapped> list = res.accept(MediaType.APPLICATION_XML).get(new GenericType<List<LogicalDataWrapped>>() {
        });
        assertNotNull(list);
        assertFalse(list.isEmpty());
    } finally {
        utils.deleteResource(testcol, false);
    }
}
Also used : DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) WebResource(com.sun.jersey.api.client.WebResource) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) List(java.util.List) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) HttpClient(org.apache.commons.httpclient.HttpClient) Client(com.sun.jersey.api.client.Client) Test(org.junit.Test)

Example 53 with DefaultClientConfig

use of com.sun.jersey.api.client.config.DefaultClientConfig in project lobcder by skoulouzis.

the class WebDAVTest method setUpClass.

@BeforeClass
public static void setUpClass() throws Exception {
    // String propBasePath = System.getProperty("user.home") + File.separator
    // + "workspace" + File.separator + "lobcder-tests"
    // + File.separator + "etc" + File.separator + "test.properties";
    String propBasePath = "etc" + File.separator + "test.properties";
    Properties prop = TestSettings.getTestProperties(propBasePath);
    String testURL = prop.getProperty("webdav.test.url", "http://localhost:8080/lobcder/dav");
    assertTrue(testURL != null);
    if (!testURL.endsWith("/")) {
        testURL = testURL + "/";
    }
    uri = URI.create(testURL);
    root = uri.toASCIIString();
    if (!root.endsWith("/")) {
        root += "/";
    }
    username = prop.getProperty(("webdav.test.username1"), "user");
    assertTrue(username != null);
    password = prop.getProperty(("webdav.test.password1"), "token0");
    assertTrue(password != null);
    quckTest = Boolean.valueOf(prop.getProperty(("test.quick"), "true"));
    int port = uri.getPort();
    if (port == -1) {
        port = 443;
    }
    ProtocolSocketFactory socketFactory = new EasySSLProtocolSocketFactory();
    Protocol https = new Protocol("https", socketFactory, port);
    Protocol.registerProtocol("https", https);
    // List authPrefs = new ArrayList();
    // authPrefs.add(AuthPolicy.BASIC);
    // client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
    client = new HttpClient();
    client.getState().setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(username, password));
    ClientConfig clientConfig = new DefaultClientConfig();
    clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
    restClient = Client.create(clientConfig);
    restClient.addFilter(new com.sun.jersey.api.client.filter.HTTPBasicAuthFilter(username, password));
    restURL = prop.getProperty(("rest.test.url"), "http://localhost:8080/lobcder/dav/rest/");
    utils = new Utils(client);
    client2 = new HttpClient();
    assertNotNull(uri.getHost());
    assertNotNull(uri.getPort());
    assertNotNull(client2);
    username2 = prop.getProperty(("webdav.test.username2"), "user2");
    assertTrue(username2 != null);
    password2 = prop.getProperty(("webdav.test.password2"), "passwd2");
    assertTrue(password2 != null);
    client2.getState().setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(username2, password2));
}
Also used : DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) Properties(java.util.Properties) EasySSLProtocolSocketFactory(org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory) EasySSLProtocolSocketFactory(org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory) ProtocolSocketFactory(org.apache.commons.httpclient.protocol.ProtocolSocketFactory) AuthScope(org.apache.commons.httpclient.auth.AuthScope) Protocol(org.apache.commons.httpclient.protocol.Protocol) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) BeforeClass(org.junit.BeforeClass)

Example 54 with DefaultClientConfig

use of com.sun.jersey.api.client.config.DefaultClientConfig in project lobcder by skoulouzis.

the class Catalogue method configureClient.

private ClientConfig configureClient() {
    TrustManager[] certs = new TrustManager[] { new X509TrustManager() {

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }
    } };
    SSLContext ctx = null;
    try {
        ctx = SSLContext.getInstance("TLS");
        ctx.init(null, certs, new SecureRandom());
    } catch (java.security.GeneralSecurityException ex) {
    }
    HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
    ClientConfig config = new DefaultClientConfig();
    try {
        config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(new HostnameVerifier() {

            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        }, ctx));
    } catch (Exception e) {
    }
    return config;
}
Also used : DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) SSLSession(javax.net.ssl.SSLSession) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) URISyntaxException(java.net.URISyntaxException) JAXBException(javax.xml.bind.JAXBException) SocketException(java.net.SocketException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) UnknownHostException(java.net.UnknownHostException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) HostnameVerifier(javax.net.ssl.HostnameVerifier) X509TrustManager(javax.net.ssl.X509TrustManager) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) HTTPSProperties(com.sun.jersey.client.urlconnection.HTTPSProperties)

Example 55 with DefaultClientConfig

use of com.sun.jersey.api.client.config.DefaultClientConfig in project incubator-atlas by apache.

the class SecureEmbeddedServerTestBase method setup.

@BeforeMethod
public void setup() throws Exception {
    jksPath = new Path(Files.createTempDirectory("tempproviders").toString(), "test.jks");
    providerUrl = JavaKeyStoreProvider.SCHEME_NAME + "://file/" + jksPath.toUri();
    String baseUrl = String.format("https://localhost:%d/", securePort);
    DefaultClientConfig config = new DefaultClientConfig();
    Client client = Client.create(config);
    client.resource(UriBuilder.fromUri(baseUrl).build());
    service = client.resource(UriBuilder.fromUri(baseUrl).build());
}
Also used : Path(org.apache.hadoop.fs.Path) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) Client(com.sun.jersey.api.client.Client) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

DefaultClientConfig (com.sun.jersey.api.client.config.DefaultClientConfig)65 ClientConfig (com.sun.jersey.api.client.config.ClientConfig)55 Client (com.sun.jersey.api.client.Client)42 WebResource (com.sun.jersey.api.client.WebResource)21 HTTPBasicAuthFilter (com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)15 ClientResponse (com.sun.jersey.api.client.ClientResponse)14 HTTPSProperties (com.sun.jersey.client.urlconnection.HTTPSProperties)9 HostnameVerifier (javax.net.ssl.HostnameVerifier)7 SSLSession (javax.net.ssl.SSLSession)7 TrustManager (javax.net.ssl.TrustManager)7 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)6 URLConnectionClientHandler (com.sun.jersey.client.urlconnection.URLConnectionClientHandler)6 IOException (java.io.IOException)6 VisibleForTesting (com.google.common.annotations.VisibleForTesting)5 ClientFilter (com.sun.jersey.api.client.filter.ClientFilter)5 LoggingFilter (com.sun.jersey.api.client.filter.LoggingFilter)5 SecureRandom (java.security.SecureRandom)5 SSLContext (javax.net.ssl.SSLContext)5 URISyntaxException (java.net.URISyntaxException)4 ArrayList (java.util.ArrayList)4