0

I have set up a REST service using JAXRS. I have configured the application like this:

WEB.XML

  <servlet>
    <description>
    JAX-RS Tools Generated - Do not modify</description>
    <servlet-name>JAX-RS Servlet</servlet-name>
    <servlet-class>com.ibm.websphere.jaxrs.server.IBMRestServlet</servlet-class>
<init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.test.gateway.service.ValidationServiceApplication</param-value>
    </init-param>
 <!--   <init-param>
            <param-name />
            <param-value />
        </init-param> -->
    <load-on-startup>1</load-on-startup>
  </servlet>

ValidationServiceApplication.java

    import java.util.HashSet;
    import java.util.Set;

    import javax.ws.rs.core.Application;


    public class ValidationServiceApplication extends Application{
        @Override
        public Set<Class<?>> getClasses() {
            Set<Class<?>> classes = new HashSet<Class<?>>();
            classes.add(ValidationService.class);
            return classes;
        }
    }

ValidationService.java

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import com.test.gateway.bo.AuthenticationManager;

@Path(value="/validationService")
public class ValidationService {

    @GET
    @Produces(value="text/plain")
    @Path(value="{token}")
    public String getPropety(@PathParam("token") String token) {
     String status = AuthenticationManager.getInstance().getTokenStatus(token);
     return status;
    }
}

In my consumer, I need to send a request to the service and get the response. I have the following code:

public String validateToken(String token){

    ClientConfig clientConfig = new ApacheHttpClientConfig();
    javax.ws.rs.core.Application app = new javax.ws.rs.core.Application() {
           public Set<Class<?>> getClasses() {
               Set<Class<?>> classes = new HashSet<Class<?>>();
               classes.add(ValidationService.class);
               return classes;
           }
    };
    clientConfig.applications(app);
    RestClient client = new RestClient(clientConfig);
    Resource resource = client.resource("http://localhost:9081/sample-server-gateway/validation/validationService/" + token);
    String response = resource.accept("text/plain").get(String.class);  
    System.err.println("the response code is: " +  response);
    return response;
}

This code is based on several examples I reviewed, however; I am not understanding the set up. If my ValidationService.java (seen in the code above), exists in my provider, how can I also access this class from my consumer? Or is it necessary to include this resource in my consumer through a JAR file or some other method? any info is appreciated.

This code seems to work but i receive a "read timed out " exception.

    public String validateToken(String token){

    ClientConfig clientConfig = new ApacheHttpClientConfig();
    RestClient client = new RestClient(clientConfig);
    Resource resource = client.resource("http://localhost:9081/sample-server-gateway/validation/validationService/" + token);
    String response = resource.accept("text/plain").get(String.class);  
    System.err.println("the response code is: " +  response);
    return response;
}

The following is the stack trace:

[2/12/18 16:54:06:591 CST] 0000008d webapp        E com.ibm.ws.webcontainer.webapp.WebApp logServletError SRVE0293E: [Servlet Error]-[ServletNameNotFound]: org.apache.wink.client.ClientRuntimeException: java.lang.RuntimeException: java.net.SocketTimeoutException: Read timed out
    at org.apache.wink.client.internal.ResourceImpl.invoke(ResourceImpl.java:240)
    at org.apache.wink.client.internal.ResourceImpl.invoke(ResourceImpl.java:189)
    at org.apache.wink.client.internal.ResourceImpl.get(ResourceImpl.java:302)
    at com.hecorp.gateway.bo.AuthenticationManager.validateToken(AuthenticationManager.java:75)
    at com.hecorp.gateway.servlet.GatewayFilter.doFilter(GatewayFilter.java:67)
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:967)
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1107)
    at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3980)
    at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:304)
    at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:1016)
    at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1817)
    at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:200)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:463)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:530)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:316)
    at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:88)
    at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)
    at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
    at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
    at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
    at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
    at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
    at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1892)
Caused by: java.lang.RuntimeException: java.net.SocketTimeoutException: Read timed out
    at org.apache.wink.client.internal.handlers.ApacheHttpClientConnectionHandler.handle(ApacheHttpClientConnectionHandler.java:82)
    at org.apache.wink.client.internal.handlers.HandlerContextImpl.doChain(HandlerContextImpl.java:52)
    at org.apache.wink.client.internal.handlers.AcceptHeaderHandler.handle(AcceptHeaderHandler.java:79)
    at org.apache.wink.client.internal.handlers.HandlerContextImpl.doChain(HandlerContextImpl.java:52)
    at org.apache.wink.client.internal.ResourceImpl.invoke(ResourceImpl.java:227)
    ... 25 more
Caused by: java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:164)
    at java.net.SocketInputStream.read(SocketInputStream.java:134)
    at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:130)
    at org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:127)
    at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:233)
    at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:98)
    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:210)
    at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:271)
    at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:227)
    at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:209)
    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:292)
    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:126)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:483)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:641)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:576)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:554)
    at org.apache.wink.client.internal.handlers.ApacheHttpClientConnectionHandler.processRequest(ApacheHttpClientConnectionHandler.java:102)
    at org.apache.wink.client.internal.handlers.ApacheHttpClientConnectionHandler.handle(ApacheHttpClientConnectionHandler.java:79)
    ... 29 more

If i hit the url from the browser I receive successful results.

enter image description here

vhdz04
  • 159
  • 2
  • 17
  • I don't quite get your question... does it work or not? If not what is the problem? Or it works, but you don't understand why? – Tarlog Feb 12 '18 at 22:40
  • I apologize, I find it difficult to explain since this is my first time dealing with apis. In short, it does not work. The validateToken method defines a custom provider and includes the class ValidationService , however this class does not exist in my consumer,, it is a class declared in the provider (the REST service). Since these are two distinct projects, I am not quite sure how to make the code work, should I recreate the ValidationService class in my consumer? – vhdz04 Feb 12 '18 at 22:47
  • I don't see any custom provider in your code. What exception do you get in client? What exception do you get in server? – Tarlog Feb 12 '18 at 22:49
  • The code will fail because the class ValidationService does not exist in my consumer (it is a class in my service), Yet I have seen it configured this way in all of the examples I have found. This leaves me wondering if my set up is correct at all. – vhdz04 Feb 12 '18 at 22:57
  • Oh, missed that part. No, you don't need add ValidationService in your client (please don't call it consumer... it's client and it calls server...) – Tarlog Feb 12 '18 at 22:59
  • Thank you, I have edited the question with some code that works and does produce an error – vhdz04 Feb 12 '18 at 23:00
  • Is your server up? Can you access "http://localhost:9081/sample-server-gateway/validation/validationService/token" from your browser? – Tarlog Feb 12 '18 at 23:02
  • yes i can, with successful results. – vhdz04 Feb 12 '18 at 23:04
  • Hmmm, weird. Looks like communication problem. Do you have/need some proxy? Try to replace localhost with 127.0.0.1 – Tarlog Feb 12 '18 at 23:10
  • If i replace with 127.0.0.1 it produces the same error. The code works from a main method. – vhdz04 Feb 12 '18 at 23:14
  • Try to replace url with google.com, does it work? it should be able to bring some text from google :) – Tarlog Feb 12 '18 at 23:15
  • google does return a response, I will take a look at my server configuration. – vhdz04 Feb 12 '18 at 23:18

1 Answers1

0

Extending the readTimeOut allowed the code to run through.

public String validateToken(String token){

    ClientConfig clientConfig = new ApacheHttpClientConfig();
    clientConfig.readTimeout(300000);
    RestClient client = new RestClient(clientConfig);
    Resource resource = client.resource("http://localhost:9081/sample-server-gateway/validation/validationService/" + token);
    String response = resource.accept(MediaType.TEXT_PLAIN).get(String.class);  
    System.err.println("the response code is: " +  response);
    return response;
}
vhdz04
  • 159
  • 2
  • 17