Search in sources :

Example 1 with AbstractHttpConnection

use of org.eclipse.jetty.server.AbstractHttpConnection in project BeyondUPnP by kevinshine.

the class AndroidJettyServletContainer method isConnectionOpen.

public static boolean isConnectionOpen(HttpServletRequest request, byte[] heartbeat) {
    Request jettyRequest = (Request) request;
    AbstractHttpConnection connection = jettyRequest.getConnection();
    Socket socket = (Socket) connection.getEndPoint().getTransport();
    if (log.isLoggable(Level.FINE))
        log.fine("Checking if client connection is still open: " + socket.getRemoteSocketAddress());
    try {
        socket.getOutputStream().write(heartbeat);
        socket.getOutputStream().flush();
        return true;
    } catch (IOException ex) {
        if (log.isLoggable(Level.FINE))
            log.fine("Client connection has been closed: " + socket.getRemoteSocketAddress());
        return false;
    }
}
Also used : Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) IOException(java.io.IOException) AbstractHttpConnection(org.eclipse.jetty.server.AbstractHttpConnection) Socket(java.net.Socket)

Example 2 with AbstractHttpConnection

use of org.eclipse.jetty.server.AbstractHttpConnection in project jena by apache.

the class FusekiErrorHandler method handle.

@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
    AbstractHttpConnection connection = AbstractHttpConnection.getCurrentConnection();
    connection.getRequest().setHandled(true);
    String method = request.getMethod();
    if (!method.equals(HttpMethods.GET) && !method.equals(HttpMethods.POST) && !method.equals(HttpMethods.HEAD))
        return;
    response.setContentType(MimeTypes.TEXT_PLAIN_UTF_8);
    response.setHeader(HttpHeaders.CACHE_CONTROL, "must-revalidate,no-cache,no-store");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(1024);
    //String writer = IO.UTF8(null) ;
    try (Writer writer = new OutputStreamWriter(bytes, "UTF-8")) {
        handleErrorPage(request, writer, connection.getResponse().getStatus(), connection.getResponse().getReason());
        if (!Fuseki.VERSION.equalsIgnoreCase("development")) {
            writer.write("\n");
            writer.write("\n");
            writer.write(format("Fuseki - version %s (Build date: %s)\n", Fuseki.VERSION, Fuseki.BUILD_DATE));
        }
        writer.flush();
    }
    response.setContentLength(bytes.size());
    // Copy
    response.getOutputStream().write(bytes.toByteArray());
}
Also used : AbstractHttpConnection(org.eclipse.jetty.server.AbstractHttpConnection)

Example 3 with AbstractHttpConnection

use of org.eclipse.jetty.server.AbstractHttpConnection in project processdash by dtuma.

the class LocalConnector method getResponse.

public ByteArrayBuffer getResponse(String uri, int port, Map extraEnv) throws Exception {
    // get the current, previously active connection if one exists
    AbstractHttpConnection conn = AbstractHttpConnection.getCurrentConnection();
    // Identify the effective server name for this request
    Request parentRequest = (conn == null ? null : conn.getRequest());
    String host = (//
    parentRequest == null ? //
    "localhost:" + port : parentRequest.getHeader("Host"));
    // construct an HTTP request for this data
    StringBuilder requestHeader = new StringBuilder();
    requestHeader.append("GET ").append(uri).append(" HTTP/1.0\r\n").append("Host: ").append(host).append("\r\n").append("Connection: close\r\n").append("Content-Length: 0\r\n\r\n");
    // execute the request and retrieve the responses
    ByteArrayBuffer requestBuffer = new ByteArrayBuffer(requestHeader.toString(), StringUtil.__ISO_8859_1);
    LocalRequest request = new LocalRequest(requestBuffer, extraEnv, parentRequest, false);
    AccessController.doPrivileged(request);
    return request.getResponsesBuffer();
}
Also used : Request(org.eclipse.jetty.server.Request) AbstractHttpConnection(org.eclipse.jetty.server.AbstractHttpConnection) ByteArrayBuffer(org.eclipse.jetty.io.ByteArrayBuffer)

Example 4 with AbstractHttpConnection

use of org.eclipse.jetty.server.AbstractHttpConnection in project elasticsearch-jetty by sonian.

the class JettyHttpServerErrorHandler method handle.

@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
    AbstractHttpConnection connection = AbstractHttpConnection.getCurrentConnection();
    connection.getRequest().setHandled(true);
    String method = request.getMethod();
    response.setContentType(MimeTypes.TEXT_PLAIN_8859_1);
    ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(4096);
    writer.write(request.getAttribute(Dispatcher.ERROR_STATUS_CODE) + " " + request.getAttribute(Dispatcher.ERROR_MESSAGE) + " " + request.getAttribute(Dispatcher.ERROR_REQUEST_URI));
    writer.flush();
    response.setContentLength(writer.size());
    writer.writeTo(response.getOutputStream());
    writer.destroy();
}
Also used : ByteArrayISO8859Writer(org.eclipse.jetty.util.ByteArrayISO8859Writer) AbstractHttpConnection(org.eclipse.jetty.server.AbstractHttpConnection)

Example 5 with AbstractHttpConnection

use of org.eclipse.jetty.server.AbstractHttpConnection in project cdap by caskdata.

the class JAASLoginService method login.

/* ------------------------------------------------------------ */
public UserIdentity login(final String username, final Object credentials) {
    try {
        CallbackHandler callbackHandler = null;
        if (callbackHandlerClass == null) {
            callbackHandler = new CallbackHandler() {

                public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                    for (Callback callback : callbacks) {
                        if (callback instanceof NameCallback) {
                            ((NameCallback) callback).setName(username);
                        } else if (callback instanceof PasswordCallback) {
                            ((PasswordCallback) callback).setPassword(credentials.toString().toCharArray());
                        } else if (callback instanceof ObjectCallback) {
                            ((ObjectCallback) callback).setObject(credentials);
                        } else if (callback instanceof RequestParameterCallback) {
                            AbstractHttpConnection connection = AbstractHttpConnection.getCurrentConnection();
                            Request request = (connection == null ? null : connection.getRequest());
                            if (request != null) {
                                RequestParameterCallback rpc = (RequestParameterCallback) callback;
                                rpc.setParameterValues(Arrays.asList(request.getParameterValues(rpc.getParameterName())));
                            }
                        } else {
                            throw new UnsupportedCallbackException(callback);
                        }
                    }
                }
            };
        } else {
            Class clazz = Loader.loadClass(getClass(), callbackHandlerClass);
            callbackHandler = (CallbackHandler) clazz.newInstance();
        }
        //set up the login context
        //TODO jaspi requires we provide the Configuration parameter
        Subject subject = new Subject();
        LoginContext loginContext = new LoginContext(loginModuleName, subject, callbackHandler, configuration);
        loginContext.login();
        //login success
        JAASUserPrincipal userPrincipal = new JAASUserPrincipal(getUserName(callbackHandler), subject, loginContext);
        subject.getPrincipals().add(userPrincipal);
        return identityService.newUserIdentity(subject, userPrincipal, getGroups(subject));
    } catch (LoginException e) {
        LOG.debug(e);
    } catch (IOException e) {
        LOG.info(e.getMessage());
        LOG.debug(e);
    } catch (UnsupportedCallbackException e) {
        LOG.info(e.getMessage());
        LOG.debug(e);
    } catch (InstantiationException e) {
        LOG.info(e.getMessage());
        LOG.debug(e);
    } catch (IllegalAccessException e) {
        LOG.info(e.getMessage());
        LOG.debug(e);
    } catch (ClassNotFoundException e) {
        LOG.info(e.getMessage());
        LOG.debug(e);
    }
    return null;
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) ObjectCallback(org.eclipse.jetty.plus.jaas.callback.ObjectCallback) Request(org.eclipse.jetty.server.Request) IOException(java.io.IOException) Subject(javax.security.auth.Subject) ObjectCallback(org.eclipse.jetty.plus.jaas.callback.ObjectCallback) RequestParameterCallback(org.eclipse.jetty.plus.jaas.callback.RequestParameterCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) LoginContext(javax.security.auth.login.LoginContext) PasswordCallback(javax.security.auth.callback.PasswordCallback) LoginException(javax.security.auth.login.LoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) RequestParameterCallback(org.eclipse.jetty.plus.jaas.callback.RequestParameterCallback) AbstractHttpConnection(org.eclipse.jetty.server.AbstractHttpConnection)

Aggregations

AbstractHttpConnection (org.eclipse.jetty.server.AbstractHttpConnection)5 Request (org.eclipse.jetty.server.Request)3 IOException (java.io.IOException)2 Socket (java.net.Socket)1 Subject (javax.security.auth.Subject)1 Callback (javax.security.auth.callback.Callback)1 CallbackHandler (javax.security.auth.callback.CallbackHandler)1 NameCallback (javax.security.auth.callback.NameCallback)1 PasswordCallback (javax.security.auth.callback.PasswordCallback)1 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)1 LoginContext (javax.security.auth.login.LoginContext)1 LoginException (javax.security.auth.login.LoginException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 ByteArrayBuffer (org.eclipse.jetty.io.ByteArrayBuffer)1 ObjectCallback (org.eclipse.jetty.plus.jaas.callback.ObjectCallback)1 RequestParameterCallback (org.eclipse.jetty.plus.jaas.callback.RequestParameterCallback)1 ByteArrayISO8859Writer (org.eclipse.jetty.util.ByteArrayISO8859Writer)1