Suggest the alternative of the Some methods which does't support jetty12
johmn123-wq opened this issue · 4 comments
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;
}
}
Please, either detail this issue a lot more (and fill in the template), or we are going to close it.
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
below I've added the migrated code. please look once
@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