com.github.ggalmazor.jfry.JettyAdapter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jetty Show documentation
Show all versions of jetty Show documentation
Java Fry is a web library inspired by Sinatra
The newest version!
package com.github.ggalmazor.jfry;
import com.github.ggalmazor.jfry.*;
import javaslang.control.Option;
import javaslang.control.Try;
import org.apache.commons.io.IOUtils;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.AbstractHandler;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
public class JettyAdapter implements JFryServer {
private final Server server;
public JettyAdapter() {
this.server = new Server();
}
@Override
public JettyAdapter atPort(int port) {
ServerConnector connector = new ServerConnector(server);
connector.setPort(port);
server.setConnectors(new Connector[]{connector});
return this;
}
@Override
public JettyAdapter onRequest(Handler handler) {
server.setHandler(new AbstractHandler() {
@Override
public void handle(String path, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
// Request creation
HttpMethod method = HttpMethod.valueOf(baseRequest.getMethod().toUpperCase());
Map headers = new HashMap<>();
Enumeration headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String name = headerNames.nextElement();
headers.put(name, request.getHeader(name));
}
Map query = decodeQueryString(request.getQueryString());
Option