All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mortbay.jetty.j2sehttpspi.J2SEHTTPContextHandler Maven / Gradle / Ivy

The newest version!
package org.mortbay.jetty.j2sehttpspi;

//========================================================================
//$$Id: JAXWS2ContextHandler.java 623 2008-01-03 21:51:42Z lorban $$
//
//------------------------------------------------------------------------
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at 
//http://www.apache.org/licenses/LICENSE-2.0
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
//========================================================================

import com.sun.net.httpserver.Authenticator;
import com.sun.net.httpserver.Authenticator.Result;
import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpPrincipal;
import org.eclipse.jetty.server.HttpConnection;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.ContextHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Jetty handler that bridges requests to {@link HttpHandler}.
 * @author lorban
 */
public class J2SEHTTPContextHandler extends ContextHandler
{

    private HttpContext _httpContext;

    private HttpHandler _httpHandler;


    public J2SEHTTPContextHandler(HttpContext httpContext, HttpHandler httpHandler)
    {
        this._httpContext = httpContext;
        this._httpHandler = httpHandler;
    }

    @Override
    public void doScope(String target, Request baseRequest, HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
    {
        if (!target.startsWith(getContextPath())) return;

        JettyHttpExchange jettyHttpExchange = new JettyHttpExchange(_httpContext, req, resp);

        // TODO: add filters processing

        try
        {
            Authenticator auth = _httpContext.getAuthenticator();
            if (auth != null)
                handleAuthentication(resp, jettyHttpExchange, auth);
            else
                _httpHandler.handle(jettyHttpExchange);
        }
        catch(Exception ex)
        {
            PrintWriter writer = new PrintWriter(jettyHttpExchange.getResponseBody());
            
            resp.setStatus(500);
            writer.println("

HTTP ERROR: 500

"); writer.println("
INTERNAL_SERVER_ERROR
"); writer.println("

RequestURI=" + req.getRequestURI() + "

"); writer.println("
");
            ex.printStackTrace(writer);
            writer.println("
"); writer.println("

Powered by jetty://

"); writer.close(); } finally { Request base_request = (req instanceof Request) ? (Request)req:HttpConnection.getCurrentConnection().getRequest(); base_request.setHandled(true); } } private void handleAuthentication(HttpServletResponse resp, JettyHttpExchange jettyHttpExchange, Authenticator auth) throws IOException { Result result = auth.authenticate(jettyHttpExchange); if (result instanceof Authenticator.Failure) { int rc = ((Authenticator.Failure)result).getResponseCode(); resp.sendError(rc); } else if (result instanceof Authenticator.Retry) { int rc = ((Authenticator.Retry)result).getResponseCode(); resp.sendError(rc); } else if (result instanceof Authenticator.Success) { HttpPrincipal principal = ((Authenticator.Success)result).getPrincipal(); jettyHttpExchange.setPrincipal(principal); _httpHandler.handle(jettyHttpExchange); } } public HttpHandler getHttpHandler() { return _httpHandler; } public void setHttpHandler(HttpHandler handler) { this._httpHandler = handler; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy