0
votes

When accessing WSO2 DAS using REST api using jersey java rest client im getting response as unsupported media type with response status 415 with response

InboundJaxrsResponse{context=ClientResponse{method=POST, uri=https://localhost:9443/analytics/search, status=415, reason=Unsupported Media Type}}

the client source below. Can anyone help on this issue.


package com.rilfi.research.c2c.das.rest.client;

import org.glassfish.jersey.SslConfigurator;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import javax.net.ssl.SSLContext;`enter code here`
import javax.ws.rs.client.*;
import javax.ws.rs.core.*;

public class DasClientApp {
    public static void main(String[] args) {
        SslConfigurator sslConfig = SslConfigurator.newInstance()
                .trustStoreFile("./client-truststore.jks")
                .trustStorePassword("wso2carbon")
                .keyStoreFile("wso2carbon.jks")
                .keyPassword("wso2carbon");
        SSLContext sslContext = sslConfig.createSSLContext();
        Client client = ClientBuilder.newBuilder().sslContext(sslContext).build();
        WebTarget webTarget = client.target("https://localhost:9443").path("analytics/search");
        MultivaluedMap<String, String> formData = new MultivaluedHashMap<String, String>();
        formData.add("tableName", "V1SALEFULL3");
        formData.add("query", "product:phone");
        formData.add("start", "0");
        formData.add("count", "10");
        Response response = webTarget.request(MediaType.APPLICATION_JSON).post(Entity.form(formData));
        System.out.println(response.getStatus());
        System.out.println(response.readEntity(String.class));
        System.out.println(response);
    }

}
1
Can try sending the request from cURL? curl -X POST -H "Content-Type: application/json" -H "Authorization: Basic YWRtaW46YWRtaW4=" -v https://localhost:9443/analytics/search -d '{"tableName":"ORG_WSO2_DAS_SAMPLE_SMART_HOME_DATA", "query":"state:Texas", "start":0, "count":3}' -k - Abimaran Kugathasan
No i need to access using java program - Rilfi
Have u added dependency in pom as this: <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>2.22.1</version> </dependency> - Samitha Chathuranga
I added above dependency still same problem - Rilfi

1 Answers

0
votes

After adding HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("admin", "admin"); i can now access wso2 DAS using rest api

package com.rilfi.research.c2c.das.rest.client;

import org.glassfish.jersey.SslConfigurator;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;

import javax.net.ssl.SSLContext;
import javax.ws.rs.client.*;
import javax.ws.rs.core.*;

/**
 * Created by rilfi on 5/5/16.
 */
public class DasClientApp {
    public static void main(String[] args) {
        SslConfigurator sslConfig = SslConfigurator.newInstance()
                .trustStoreFile("client-truststore.jks")
                .trustStorePassword("wso2carbon")
                .keyStoreFile("wso2carbon.jks")
                .keyPassword("wso2carbon");
        SSLContext sslContext = sslConfig.createSSLContext();
        HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("admin", "admin");
        Client client = ClientBuilder.newBuilder().sslContext(sslContext).build();
        WebTarget webTarget = client.target("https://localhost:9443").path("analytics/search").register(feature);
        String payload = "{\"tableName\":\"V1SALEFULL3\",\"query\":\"product:phone\",\"start\":50,\"count\":10}";
        Response response = webTarget.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(payload));
        System.out.println(response.getStatus());
        System.out.println(response.readEntity(String.class));
        System.out.println(response);
    }

}