Search in sources :

Example 1 with HttpStatus

use of org.glassfish.grizzly.http.util.HttpStatus in project Payara by payara.

the class AdminAdapter method onMissingResource.

/**
 * Call the service method, and notify all listeners
 *
 * @exception Exception if an error happens during handling of
 *   the request. Common errors are:
 *   <ul><li>IOException if an input/output error occurs and we are
 *   processing an included servlet (otherwise it is swallowed and
 *   handled by the top level error handler mechanism)
 *       <li>ServletException if a servlet throws an exception and
 *  we are processing an included servlet (otherwise it is swallowed
 *  and handled by the top level error handler mechanism)
 *  </ul>
 *  Tomcat should be able to handle and log any other exception ( including
 *  runtime exceptions )
 */
@Override
public void onMissingResource(Request req, Response res) {
    LogHelper.getDefaultLogger().log(Level.FINER, "Received something on {0}", req.getRequestURI());
    LogHelper.getDefaultLogger().log(Level.FINER, "QueryString = {0}", req.getQueryString());
    HttpStatus statusCode = HttpStatus.OK_200;
    String requestURI = req.getRequestURI();
    /*    if (requestURI.startsWith("/__asadmin/ADMINGUI")) {
            super.service(req, res);

        }*/
    ActionReport report = getClientActionReport(requestURI, req);
    // remove the qualifier if necessary
    if (requestURI.indexOf('.') != -1) {
        requestURI = requestURI.substring(0, requestURI.indexOf('.'));
    }
    Payload.Outbound outboundPayload = PayloadImpl.Outbound.newInstance();
    try {
        if (!latch.await(20L, TimeUnit.SECONDS)) {
            report = getClientActionReport(req.getRequestURI(), req);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setMessage("V3 cannot process this command at this time, please wait");
        } else {
            final Subject s = (authenticator == null) ? null : authenticator.loginAsAdmin(req);
            if (s == null) {
                reportAuthFailure(res, report, "adapter.auth.userpassword", "Invalid user name or password", HttpURLConnection.HTTP_UNAUTHORIZED, "WWW-Authenticate", "BASIC");
                return;
            }
            report = doCommand(requestURI, req, report, outboundPayload, s);
        }
    } catch (ProcessHttpCommandRequestException reqEx) {
        report = reqEx.getReport();
        statusCode = reqEx.getResponseStatus();
    } catch (InterruptedException e) {
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage("V3 cannot process this command at this time, please wait");
    } catch (Exception e) {
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage("Exception while processing command: " + e);
    }
    try {
        res.setStatus(statusCode);
        /*
             * Format the command result report into the first part (part #0) of
             * the outbound payload and set the response's content type based
             * on the payload's.  If the report is the only part then the
             * stream will be written as content type text/something and
             * will contain only the report.  If the payload already has
             * content - such as files to be downloaded, for example - then the
             * content type of the payload reflects its multi-part nature and
             * an implementation-specific content type will be set in the response.
             */
        ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
        report.writeReport(baos);
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        final Properties reportProps = new Properties();
        reportProps.setProperty("data-request-type", "report");
        outboundPayload.addPart(0, report.getContentType(), "report", reportProps, bais);
        res.setContentType(outboundPayload.getContentType());
        String commandName = req.getRequestURI().substring(getContextRoot().length() + 1);
        // Check session routing for commands that have @ExecuteOn(RuntimeType.SINGLE_INSTANCE)
        if (isSingleInstanceCommand(commandName)) {
            res.addHeader(SET_COOKIE_HEADER, getCookieHeader(req));
        }
        outboundPayload.writeTo(res.getOutputStream());
        res.getOutputStream().flush();
        res.finish();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : HttpStatus(org.glassfish.grizzly.http.util.HttpStatus) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ActionReport(org.glassfish.api.ActionReport) Properties(java.util.Properties) Subject(javax.security.auth.Subject) LoginException(javax.security.auth.login.LoginException) RemoteAdminAccessException(org.glassfish.internal.api.RemoteAdminAccessException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) Payload(org.glassfish.api.admin.Payload)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Properties (java.util.Properties)1 Subject (javax.security.auth.Subject)1 LoginException (javax.security.auth.login.LoginException)1 ActionReport (org.glassfish.api.ActionReport)1 Payload (org.glassfish.api.admin.Payload)1 HttpStatus (org.glassfish.grizzly.http.util.HttpStatus)1 RemoteAdminAccessException (org.glassfish.internal.api.RemoteAdminAccessException)1