Category Archives: apache-wink

how i used apache wink client request webserivce server

here is my client code ,i used apache wink client in here

public class HttpUtilWink {
protected static final String PRE_URLRS = PropertyUtils.get("appServer");
private static Logger LOG = Logger.getLogger(HttpUtilWink.class);

public static ClientResponse sendRequest(String uri, Object obj, String method) {

    RestClient client = new RestClient();
    Resource resource = client.resource(PRE_URLRS + uri);
    ClientResponse response;
    resource.header("Accept", "application/json");
    if (method.equals("GET")) {
        response = resource.get();
    } else {
        response = resource.contentType(MediaType.APPLICATION_FORM_URLENCODED)
            .accept(MediaType.APPLICATION_JSON).post(serialize(bean));
    }
    return response;
}
static final Bean bean = new Bean(1161,1);
public static void main(String[] a) {
    String uri = "inviteCode/createInviteCode";
    Map<String, Object> queryParam = new HashMap<String, Object>();
    queryParam.put("count", 333);
    queryParam.put("amount", 1);
    ClientResponse response = HttpUtilWink.sendRequest(uri, new Bean(11, 1), "POST");
    String errorMessage = response.getMessage();
    System.out.println(errorMessage);
}

/**
 * 
 *
 * @param obj
 * @return
 */
public static byte[] serialize(Object obj) {
    ByteArrayOutputStream baos = null;
    ObjectOutputStream oos = null;
    byte[] bytes = null;
    try {
        baos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        bytes = baos.toByteArray();
        oos.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bytes;
}

public static class Bean implements Serializable {
    private int count;
    private int amount;

    public Bean(int count, int amount) {
        this.count = count;
        this.amount = amount;
    }
}}

this is my server code,my server code use jersery.

/**
 * <p>
 * create
 * </p>
 *
 * @param amount 
 * @return
 */
@POST
@Path("/createInviteCode")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "create invite code",
    response = Response.class, httpMethod = "POST")
@ApiResponses(value = {@ApiResponse(code = 404, message = "error")})
public Response createInviteCode(
    @ApiParam(value = "count", required = true)
    @FormParam("count")
    int count,
    @ApiParam(value = "amount", required = true)
    @FormParam("amount")
    int amount) {
    try {
        if (count < 1 || count > 100000) {
            return new Response(ErrorCode.PARAM_ERROR);
        }
        for (int i = 0; i < count; i++) {
            inviteCodeService.createInviteCode(amount);
        }
    } catch (BusinessException e) {
        LOG.info(
            "Business Exception: [code]= " + e.getErrorCode().getCode() + " [messgae]= " + e
                .getErrorCode().getMsg());
        return new Response(e.getErrorCode());
    } catch (Exception e) {
        LOG.error("Exception: ", e);
        return new Response(ErrorCode.FAILURE);
    }
    return new Response();
}

my question : on my server , the count and amount is zero. How can i update my code,let my code right.

how i used apache wink client request webserivce server

here is my client code ,i used apache wink client in here

public class HttpUtilWink {
protected static final String PRE_URLRS = PropertyUtils.get("appServer");
private static Logger LOG = Logger.getLogger(HttpUtilWink.class);

public static ClientResponse sendRequest(String uri, Object obj, String method) {

    RestClient client = new RestClient();
    Resource resource = client.resource(PRE_URLRS + uri);
    ClientResponse response;
    resource.header("Accept", "application/json");
    if (method.equals("GET")) {
        response = resource.get();
    } else {
        response = resource.contentType(MediaType.APPLICATION_FORM_URLENCODED)
            .accept(MediaType.APPLICATION_JSON).post(serialize(bean));
    }
    return response;
}
static final Bean bean = new Bean(1161,1);
public static void main(String[] a) {
    String uri = "inviteCode/createInviteCode";
    Map<String, Object> queryParam = new HashMap<String, Object>();
    queryParam.put("count", 333);
    queryParam.put("amount", 1);
    ClientResponse response = HttpUtilWink.sendRequest(uri, new Bean(11, 1), "POST");
    String errorMessage = response.getMessage();
    System.out.println(errorMessage);
}

/**
 * 
 *
 * @param obj
 * @return
 */
public static byte[] serialize(Object obj) {
    ByteArrayOutputStream baos = null;
    ObjectOutputStream oos = null;
    byte[] bytes = null;
    try {
        baos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        bytes = baos.toByteArray();
        oos.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bytes;
}

public static class Bean implements Serializable {
    private int count;
    private int amount;

    public Bean(int count, int amount) {
        this.count = count;
        this.amount = amount;
    }
}}

this is my server code,my server code use jersery.

/**
 * <p>
 * create
 * </p>
 *
 * @param amount 
 * @return
 */
@POST
@Path("/createInviteCode")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "create invite code",
    response = Response.class, httpMethod = "POST")
@ApiResponses(value = {@ApiResponse(code = 404, message = "error")})
public Response createInviteCode(
    @ApiParam(value = "count", required = true)
    @FormParam("count")
    int count,
    @ApiParam(value = "amount", required = true)
    @FormParam("amount")
    int amount) {
    try {
        if (count < 1 || count > 100000) {
            return new Response(ErrorCode.PARAM_ERROR);
        }
        for (int i = 0; i < count; i++) {
            inviteCodeService.createInviteCode(amount);
        }
    } catch (BusinessException e) {
        LOG.info(
            "Business Exception: [code]= " + e.getErrorCode().getCode() + " [messgae]= " + e
                .getErrorCode().getMsg());
        return new Response(e.getErrorCode());
    } catch (Exception e) {
        LOG.error("Exception: ", e);
        return new Response(ErrorCode.FAILURE);
    }
    return new Response();
}

my question : on my server , the count and amount is zero. How can i update my code,let my code right.

Apache – org.apache.http.NoHttpResponseException on httpClient.execute

I have written a servlet code in Google App Engine using Apache Wink. The code runs fine when I run it locally but when I deploy it to App Engine, I see intermittent org.apache.http.NoHttpResponseException exceptions on httpClient.execute

The servlet is being run as a Singleton. Is there any error in my connection pooling code? Here is my servlet code below.

@org.apache.wink.common.annotations.Scope(ScopeType.SINGLETON)
@Path("/")
public class SparkResource implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -5278229852877855876L;
    private static final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
    private static final CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connManager).build();
    private final Logger log = Logger.getLogger(RailwayResource.class.getName());

    @GET
    @Path("spark")
    @Produces({MediaType.APPLICATION_JSON}) 
    public String readSparkStatus(@QueryParam("param1") String param1 throws Exception {

        HttpPost httpPost = new HttpPost("http://www.spark.com");

        List <NameValuePair> nvps = new ArrayList <NameValuePair>();    

        nvps.add(new BasicNameValuePair("param1", param1));

        httpPost.setEntity(new UrlEncodedFormEntity(nvps));

        Document doc = null;
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            doc = Jsoup.parse(entity.getContent(), "UTF-8", "");
            EntityUtils.consume(entity);
        } catch(Exception ex) {
            log.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            if(response != null) {
                response.close();
            }
        }

        ...
    }
}

What am I doing wrong here? Is my usage of PoolingHttpClientConnectionManager correct? My app may receive about 200 requests/min in peak hours. Does the above code look efficient to handling this?

Thank you

Migrating from Apache Wink to jaxrs-2.0 impl

It looks as if Apache Wink project is dead and there is more emphasis on Apache CXF project. I have been using Apache Wink in my REST project but due to lack of activity and non-adoption of jaxrs-2.0 specification, I am considering migrating my project to Jersey or any other jaxrs-2.0 implementation.

Request your suggestion in choosing the right jaxrs-2.0 implementation and migration challenges. I have used some wink specific stuff such as applicationConfigLocation as init param in web.xml etc.

For instance, does Jersey support resource an and provider registration through properties file using applicationConfigLocation. Otherwise, what is best alternate approach to migrate those configurations. Other than this I have always retrained from using wink specific classes or features. But for now, there is a need to use ContainerResponseFilter that was introduced in jaxrs-2.0 (which is not available in Apache wink).

How to fix this Apache Wink issue?

I am getting following error while converting TreeMap into JSON object with WINK.

09:46:54.980 [http-apr-8080-exec-5] ERROR o.a.w.s.i.h.FlushResultHandler - The system could not find a javax.ws.rs.ext.MessageBodyWriter or a DataSourceProvider class for the java.util.TreeMap type and application/json mediaType. Ensure that a javax.ws.rs.ext.MessageBodyWriter exist s in the JAX-RS application for the type and media type specified. 09:46:54.989 [http-apr-8080-exec-5] DEBUG o.a.w.server.internal.log.Responses - There were no custom headers written on the response. 09:46:54.991 [http-apr-8080-exec-5] DEBUG o.a.w.server.internal.log.Responses - The response entity was not written to the HttpServletResponse.getOutputStream(). 09:46:55.004 [http-apr-8080-exec-5] DEBUG o.a.w.s.internal.RequestProcessor - The following error occurred during the invocation of the handlers chain: WebApplicationException (406 - Not Acceptable) with message 'null' while processing GET request sent to... javax.ws.rs.WebApplicationException: null at org.apache.wink.server.internal.handlers.FlushResultHandler.handleResponse(FlushResultHandler.java:240) ~[wink-server-1.4.jar:1.4]

My POM.xml has WINK JSON jar.

<dependency>
    <groupId>org.apache.wink</groupId>
        <artifactId>wink-common</artifactId>
        <version>1.4</version>
</dependency>
<dependency>
    <groupId>org.apache.wink</groupId>
    <artifactId>wink-server</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
    <groupId>org.apache.wink</groupId>
    <artifactId>wink-json4j</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
    <groupId>org.apache.wink</groupId>
    <artifactId>wink-common</artifactId>
    <version>1.4</version>
</dependency>

How to return a simple JSP with Apache Wink

In my project, I had used Glassfish JAX-RS jars that came with a Viewable object. Now, I nam migrating to Apache Wink and could not find a simple way of returning a JSP. What is the simplest way of doing this?

Previously (with org.glassfish.jersey.server.mvc.Viewable object) -

@GET
public Viewable get() {
    System.out.println("In UAPIMainController");
    request.setAttribute("rootContext", request.getContextPath());
    return new Viewable("/WEB-INF/pages/main.jsp");
}

How do I achieve this with Apache Wink?