Bind J2SE Endpoint to HTTPS

时光怂恿深爱的人放手 提交于 2019-12-11 07:35:54

问题


I created a basic REST service that works when it is invoked over a http connection. But when I try to add SSL on top I have some problems.

This is the code for the server creation:

 private HttpContext getHttpContext() {
  HttpsServer server = null;
  try {
   server = HttpsServer.create(new InetSocketAddress("localhost", 443), 5);

   server.setHttpsConfigurator(new HttpsConfigurator(SecureChatSslContextFactory.getServerContext()) {

    public void configure(HttpsParameters params) {
     SSLContext context = getSSLContext();

     // get the default parameters
     SSLParameters sslparams = context.getDefaultSSLParameters();
     params.setSSLParameters(sslparams);
    }
   });

   server.start();

   return server.createContext("/customerservice/customer");
  } catch (IOException e) {
   e.printStackTrace();
  }
  return null;
 } 
    Endpoint e = Endpoint.create(HTTPBinding.HTTP_BINDING, new RestSourcePayloadProvider()); 
    e.publish(getHttpContext());

The class RestSourcePayloadProvider has the public method invoke, but it never gets called. I suppose that the reason for this behaviour is that the binding for the endpoint is HTTPBinding.HTTP_BINDING, and not HTTPS. But I didn't manage to find a way to bind to https.

If I run the test client or the browser on link text I get the same answer:

500 Internal Server Error No handler for context


回答1:


I found the answer to my problem. I was not creating a handler for the incoming requests. I was supposed to use

return server.createContext("/customerservice/customer", new HttpHandler(...));

instead of

return server.createContext("/customerservice/customer");

It looks like the interface Endpoint is implemented by the EndpointImplclass in Apache's CFX. When calling publish(address), the method called in EndpointImpl creates a ServerImpl that will be the service server. If I call publish(server context), nothing happens because EndpointImpl doesn't override that method also.




回答2:


For those who want to use the CFX's EndpointImpl and SSL try this: link text

It uses the Jetty Server package to create the working server and publish it to the endpoint.



来源:https://stackoverflow.com/questions/4735116/bind-j2se-endpoint-to-https

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!