Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest the alternative of the Some methods which does't support jetty12 #12157

Open
johmn123-wq opened this issue Aug 12, 2024 · 4 comments
Open
Labels
Bug For general bugs on Jetty side

Comments

@johmn123-wq
Copy link

johmn123-wq commented Aug 12, 2024

Jetty version(s)
migration from Jetty 11.0.x to Jetty 12.0.x

Jetty Environment
Applicable for jetty-12 ee10

Java version/vendor (use: java -version)
Java17

OS type/version

Description
Here is the code for 11.0.x

public class ApacheServerHandler extends AbstractHandler{

   private ApacheServerHandler(){
       client = HttpClients.createDeafult();
   }

@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{

  String newHost = request.getHeader('sb_host');
 if(newHost.endsWith("/")){
     newHost  = StringUtils.substring(newHost, 0, newHost.length() - 1);  
  }

HttpUriRequest = null;
String method = request.getMethod();
String queryString = getQueryString(baseRequest);
String path = baseRequest.getPathInfo();
Enumeration<String>headers = request.getHeaderNames();

String newUrl = String.format("http://%s%s", newHost, path, queryString);

if("GET".equalsIgnoreCase(method){
  uriRequest = new HttpGet(newUrl);
}
else if("POST".equalsIgnoreCase(method){
  uriRequest = new HttpPost(newUrl);
}
else if("PUT".equalsIgnoreCase(method){
  uriRequest = new HttpPut(newUrl);
}
else{
  baseRequest.setHandled(false);
}

if(uriRequest instanceof HttpEntityEncloseingRequest){
  HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) uriRequest;
  entityRequest.setEntity(new InputStreamEntity(request.getInputStream()));
}

while(headers.hasMoreElements()){
    String headerName = headers.nextElement();
    if(headerName.equalIgnoreCase('Content-length'))
       continue;
    uriRequest.addHeader(headerName, baseRequest.getHeader(headerName));

    try(ClosableHttpResponse resp = client.execute(uriRequest)){
         for(Header header: resp.getAllHeaders){
                response.addHeader(header.getName(), header.getValue());
         } 
         String body = EntityUtils.toString(resp.getEntity());
         reponse.getOutputStream().write(body.getBytes())
   }

  baseRequest.setHandled(true);

}

private String getQueryString(Request request){
   
      String queryString = baseRequest.getQueryString();
      if(queryString == null){
              return "";
       }
      return queryString;
 }

}

Here is the migrated code to jetty12 where getOutStream, setHandled, response.add() etc functions are not supported.

public class ApacheServerHandler extends Handler.Abstract{

private ApacheServerHandler(){
client = HttpClients.createDeafult();
}

@OverRide
public void handle(Request request, Response response, Callback callback) throws IOException, ServletException{

String newHost = request.getHeaders().get('sb_host');
if(newHost.endsWith("/")){
newHost = StringUtils.substring(newHost, 0, newHost.length() - 1);
}

HttpUriRequest = null;
String method = request.getMethod();
String queryString = request.getHttpURI().getQuery();
String path = request.getHttpURI().getPath();
Enumerationheaders = request.getHeaders().getFieldNames();

String newUrl = String.format("http://%s%s", newHost, path, queryString);

if("GET".equalsIgnoreCase(method){
uriRequest = new HttpGet(newUrl);
}
else if("POST".equalsIgnoreCase(method){
uriRequest = new HttpPost(newUrl);
}
else if("PUT".equalsIgnoreCase(method){
uriRequest = new HttpPut(newUrl);
}
else{
baseRequest.setHandled(false);
}

if(uriRequest instanceof HttpEntityEncloseingRequest){
HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) uriRequest;
entityRequest.setEntity(new InputStreamEntity(Request.asInputStream(requrest), ContentType.create(request.getHeaders().getHtHeader.CONTENT_TYPE.asString()))));
}

while(headers.hasMoreElements()){
String headerName = headers.nextElement();
if(headerName.equalIgnoreCase('Content-length'))
continue;
uriRequest.addHeader(headerName, baseRequest.getHeader(headerName));

try(ClosableHttpResponse resp = client.execute(uriRequest)){
     for(Header header: resp.getAllHeaders){
            response.**add**(header.**getName**(), header.**getValue**());
     } catch(ParseException e){
            throw new IOException(e);
 } 

     String body = EntityUtils.toString(resp.getEntity());
     reponse.getOutputStream().write(body.getBytes())

}

baseRequest.setHandled(true);
return false;

}

}

@johmn123-wq johmn123-wq added the Bug For general bugs on Jetty side label Aug 12, 2024
@johmn123-wq johmn123-wq changed the title Suggest the alternative of the Some methods which doesn Suggest the alternative of the Some methods which does't support jetty12 Aug 12, 2024
@sbordet
Copy link
Contributor

sbordet commented Aug 12, 2024

Please, either detail this issue a lot more (and fill in the template), or we are going to close it.

@joakime
Copy link
Contributor

joakime commented Aug 12, 2024

extends AbstractHandler

That doesn't exist on Jetty 12.

public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{

That method signature does not exist on Jetty 12.

See: https://jetty.org/docs/jetty/12/programming-guide/migration/11-to-12.html

@johmn123-wq
Copy link
Author

below I've added the migrated code. please look once

@janbartel
Copy link
Contributor

@johmn123-wq I think this documentation page on jetty 12 handlers will help you enormously: https://jetty.org/docs/jetty/12/programming-guide/server/http.html#handler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug For general bugs on Jetty side
Projects
None yet
Development

No branches or pull requests

4 participants