Example usage for javax.servlet.http HttpServletResponse getStatus

List of usage examples for javax.servlet.http HttpServletResponse getStatus

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse getStatus.

Prototype

public int getStatus();

Source Link

Document

Gets the current status code of this response.

Usage

From source file:com.groupon.odo.Proxy.java

/**
 * Apply enabled response overrides/* ww w.j a  va2  s  .c  o m*/
 *
 * @param httpServletResponse
 * @param httpServletRequest
 * @throws Exception
 */
private void applyResponseOverrides(HttpServletResponse httpServletResponse,
        HttpServletRequest httpServletRequest, History history) throws Exception {
    RequestInformation requestInfo = requestInformation.get();
    for (EndpointOverride selectedPath : requestInfo.selectedResponsePaths) {
        // check to see if there is custom override data
        // something like
        List<EnabledEndpoint> points = selectedPath.getEnabledEndpoints();
        for (EnabledEndpoint endpoint : points) {
            // skip if the repeat number is 0
            if (endpoint.getRepeatNumber() == 0)
                continue;

            // skip if this is a custom override and one has already been applied
            if (endpoint.getOverrideId() == Constants.PLUGIN_RESPONSE_OVERRIDE_CUSTOM
                    && requestInfo.usedCustomResponse)
                continue;

            // decrease repeat number
            endpoint.decrementRepeatNumber();

            if (endpoint.getOverrideId() == Constants.PLUGIN_RESPONSE_OVERRIDE_CUSTOM) {
                // return custom response
                requestInfo.outputString = endpoint.getArguments()[0].toString();
                httpServletResponse.setContentType(selectedPath.getContentType());
                requestInformation.get().usedCustomResponse = true;
                requestInformation.get().modified = true;
            } else if (endpoint.getOverrideId() == Constants.PLUGIN_RESPONSE_HEADER_OVERRIDE_ADD) {
                httpServletResponse = HttpUtilities.addHeader(httpServletResponse, endpoint.getArguments());
                requestInformation.get().modified = true;
            } else if (endpoint.getMethodInformation() != null && endpoint.getMethodInformation()
                    .getMethodType().equals(Constants.PLUGIN_TYPE_RESPONSE_OVERRIDE)) {
                // run method
                try {
                    com.groupon.odo.proxylib.models.Method methodInfo = PathOverrideService.getInstance()
                            .getMethodForEnabledId(endpoint.getOverrideId());

                    logger.info("method = {}", methodInfo);
                    logger.info("Enabled endpoint: {}", methodInfo.getMethodName());

                    logger.info("Calling override for {} with data: {}",
                            httpServletRequest.getRequestURL() + "?" + httpServletRequest.getQueryString(),
                            requestInfo.outputString);
                    requestInfo.outputString = (String) PluginManager.getInstance().callFunction(
                            methodInfo.getClassName(), methodInfo.getMethodName(), requestInfo.outputString,
                            endpoint.getArguments());

                    // check the return code
                    logger.info("Http code should be {}", methodInfo.getHttpCode());
                    if (methodInfo.getHttpCode() != httpServletResponse.getStatus()) {
                        logger.info("Setting HTTP Code to {}", methodInfo.getHttpCode());
                        httpServletResponse.setStatus(methodInfo.getHttpCode());

                        // the server might have set a "Status" header.. so let's reset this too
                        httpServletResponse.setHeader(Constants.HEADER_STATUS,
                                Integer.toString(methodInfo.getHttpCode()));
                    }
                    requestInformation.get().modified = true;
                    logger.info("Done calling override");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

From source file:com.att.ajsc.csilogging.common.CSILoggingUtils.java

public void finalizeRequest(HttpServletRequest request, HttpServletResponse response) {

    logger.debug("In...:finalizeRequest");
    String servicename = UtilLib.getServiceName(request);

    PerformanceTrackingBean perfTrackerBean = (PerformanceTrackingBean) request
            .getAttribute(PERFORMANCE_TRACKER_BEAN);
    long startTime = (long) request.getAttribute(CommonNames.START_TIME);
    AuditRecord ar = new AuditRecord();
    try {/* w w w  .j a va  2  s.  c o  m*/
        logger.debug("Starting application specific handling...:finalizeRequest");
        // request.setAttribute(CommonNames.AUDIT_RECORD, ar);
        // request.setAttribute(CommonNames.ATTR_START_TIME,
        // Long.valueOf(startTime).toString());
        perfTrackerBean.setAuditRecord(ar);
        servicename = LoggerNameConverter.convertNormalizedName(request, servicename);

        perfTrackerBean.setServiceName(servicename);

        perfTrackerBean.setRequestContentLen(request.getContentLength());
        perfTrackerBean.setResponseMsgSize(getResponseLength(request));
        perfTrackerBean.setMethod(request.getMethod());

        ar.setInstanceName(SystemParams.instance().getInstanceName());
        ar.setInitiatedTimestamp(UtilLib.epochToXmlGC(startTime));
        ar.setVtier(SystemParams.instance().getVtier());
        ar.setCluster(SystemParams.instance().getCluster());
        ar.setHostName(SystemParams.instance().getHostName());
        ar.setHostIPAddress(SystemParams.instance().getIpAddress());
        ar.setSubject("CW.pub.spm2." + servicename + ".response");
        ar.setMode("");
        ar.setServiceKeyData1("");
        ar.setServiceKeyData2("");
        ar.setSourceClass(CommonNames.SOURCE_CLASS);
        ar.setSourceMethod(CommonNames.AUDIT_LOGGER_NAME);
        ar.setTransactionName(servicename);
        /*
         * ar.setApplicationId(request.getAttribute(CommonNames.
         * CSI_USER_NAME));
         * ar.setConversationId(request.getAttribute(CommonNames.
         * CSI_CONVERSATION_ID));
         * ar.setUniqueTransactionId(request.getAttribute(CommonNames.
         * CSI_UNIQUE_TXN_ID));
         * ar.setOriginalMessageId(request.getAttribute(CommonNames.
         * CSI_MESSAGE_ID));
         * ar.setOriginatorId(request.getAttribute(CommonNames.
         * CSI_ORIGINATOR_ID));
         * ar.setClientApp(UtilLib.ifNullThenEmpty(request.getAttribute(
         * CommonNames.CSI_CLIENT_APP))); ar.setOriginationSystemId("N/A");
         * ar.setOriginationSystemName(request.getAttribute(CommonNames.
         * CSI_USER_NAME));
         * ar.setOriginationSystemVersion(request.getAttribute(CommonNames.
         * CSI_VERSION));
         */

        ar.setApplicationId(perfTrackerBean.getUserName());
        ar.setConversationId(perfTrackerBean.getConversationId());
        ar.setUniqueTransactionId(perfTrackerBean.getUniqueTransactionId());
        ar.setOriginalMessageId(perfTrackerBean.getOriginalMessageId());
        ar.setOriginatorId(perfTrackerBean.getOriginatorId());
        ar.setClientApp(UtilLib.ifNullThenEmpty(perfTrackerBean.getClientApp()));
        ar.setOriginationSystemId("N/A");
        ar.setOriginationSystemName(perfTrackerBean.getUserName());
        ar.setOriginationSystemVersion(perfTrackerBean.getOriginationSystemVersion());

        // new fields added per new schema
        ar.setClientIP(request.getRemoteAddr());
        ar.setHttpMethod(perfTrackerBean.getMethod());
        ar.setRequestURL(request.getPathInfo());

        // PerformanceTracking.initPerfTrack(request,servicename);
        PerformanceTracking.initPerfTrack(perfTrackerBean, servicename);
        // PerformanceTracking.addPerfTrack(request, "Main", "I",
        // startTime.toString(), servicename);

        int httpCode = response.getStatus();

        if (httpCode == HttpServletResponse.SC_UNAUTHORIZED) {
            ar.setResponseCode(CommonNames.CSI_AUTH_ERROR);
            ar.setResponseDescription(CommonErrors.DEF_401_FAULT_DESC);
            ar.setFaultCode(CommonErrors.DEF_401_FAULT_CODE);
            ar.setFaultDescription(CommonErrors.DEF_401_FAULT_DESC);
            ar.setFaultLevel("ERROR");
            ar.setTransactionStatus("E");
            ar.setFaultEntity("CSI");
            ar.setFaultTimestamp(UtilLib.epochToXmlGC(System.currentTimeMillis()));
            ar.setExternalFaultCode(String.valueOf(httpCode));
            ar.setExternalFaultDescription(CommonErrors.GENERIC_XML_ERROR);
        } else if (httpCode == HttpServletResponse.SC_FORBIDDEN) {
            ar.setResponseCode(CommonNames.CSI_AUTH_ERROR);
            ar.setResponseDescription(CommonErrors.DEF_403_FAULT_DESC);
            ar.setFaultCode(CommonErrors.DEF_403_FAULT_CODE);
            ar.setFaultDescription(CommonErrors.DEF_403_FAULT_DESC);
            ar.setFaultLevel("ERROR");
            ar.setTransactionStatus("E");
            ar.setFaultEntity("CSI");
            ar.setFaultTimestamp(UtilLib.epochToXmlGC(System.currentTimeMillis()));
            ar.setExternalFaultCode(String.valueOf(httpCode));
            ar.setExternalFaultDescription(CommonErrors.GENERIC_XML_ERROR);
        } else if (httpCode == HttpServletResponse.SC_NOT_IMPLEMENTED) {

            ar.setResponseCode(CommonNames.CSI_SERVICE_UNAVAIL_ERROR);
            ar.setResponseDescription(CommonErrors.DEF_501_FAULT_DESC);
            ar.setFaultCode(CommonErrors.DEF_501_FAULT_CODE);
            ar.setFaultDescription(CommonErrors.DEF_501_FAULT_DESC);
            ar.setFaultLevel("ERROR");
            ar.setTransactionStatus("E");
            ar.setFaultEntity("CSI");
            ar.setFaultTimestamp(UtilLib.epochToXmlGC(System.currentTimeMillis()));
            ar.setExternalFaultCode(String.valueOf(httpCode));
            ar.setExternalFaultDescription(CommonErrors.GENERIC_XML_ERROR);

        } else if (httpCode == HttpServletResponse.SC_SERVICE_UNAVAILABLE) {
            ar.setResponseCode(CommonNames.CSI_SERVICE_UNAVAIL_ERROR);
            ar.setResponseDescription(CommonErrors.DEF_503_FAULT_DESC);
            ar.setFaultCode(CommonErrors.DEF_503_FAULT_CODE);
            ar.setFaultDescription(CommonErrors.DEF_503_FAULT_DESC);
            ar.setFaultLevel("ERROR");
            ar.setTransactionStatus("E");
            ar.setFaultEntity("CSI");
            ar.setFaultTimestamp(UtilLib.epochToXmlGC(System.currentTimeMillis()));
            ar.setExternalFaultCode(String.valueOf(httpCode));
            ar.setExternalFaultDescription(CommonErrors.GENERIC_XML_ERROR);
        } else if (400 <= httpCode && httpCode <= 499) {

            ar.setResponseCode(CommonNames.CSI_SERVICE_UNAVAIL_ERROR);
            ar.setResponseDescription(CommonErrors.DEF_4NN_FAULT_DESC);
            ar.setFaultCode(CommonErrors.DEF_4NN_FAULT_CODE);
            ar.setFaultDescription(CommonErrors.DEF_4NN_FAULT_DESC);
            ar.setFaultLevel("ERROR");
            ar.setFaultEntity("CSI");
            ar.setTransactionStatus("E");
            ar.setFaultTimestamp(UtilLib.epochToXmlGC(System.currentTimeMillis()));
            ar.setExternalFaultCode(String.valueOf(httpCode));
            ar.setExternalFaultDescription(CommonErrors.GENERIC_XML_ERROR);

        } else if (httpCode == 500) {

            ar.setResponseCode(CommonNames.CSI_SERVICE_UNAVAIL_ERROR);
            ar.setResponseDescription(CommonErrors.DEF_500_FAULT_DESC);
            ar.setFaultCode(CommonErrors.DEF_500_FAULT_CODE);
            ar.setFaultDescription(CommonErrors.DEF_500_FAULT_DESC);
            ar.setFaultLevel("ERROR");
            ar.setFaultEntity("CSI");
            ar.setTransactionStatus("E");
            // ar.setFaultTimestamp(UtilLib.epochToXmlGC((new
            // Double(System.nanoTime()/1000000)).longValue()));
            ar.setFaultTimestamp(UtilLib.epochToXmlGC(System.currentTimeMillis()));
            ar.setExternalFaultCode(String.valueOf(httpCode));
            ar.setExternalFaultDescription(CommonErrors.GENERIC_XML_ERROR);

        }

        else {
            ar.setResponseDescription(CommonNames.CSI_SUCCESS);
            ar.setResponseCode(CommonNames.CSI_SUCCESS_RESPONSE_CODE);
            ar.setTransactionStatus("C");
        }

        // Enhance CSI logging to use the CAET error code

        if (response.getHeader(CommonNames.CAET_RestErrorCode) != null
                || response.getHeader(CommonNames.CAET_CingularErrorCode) != null) {

            // if(request.getHeader("X-CAET-CingularErrorCode") != null){
            if ("Y".equals(request.getAttribute(CommonNames.AJSC_CAET_IS_REST_SERVICE))) {

                ar.setResponseCode(response.getHeader(CommonNames.CAET_CingularErrorCategory));
                ar.setResponseDescription(response.getHeader(CommonNames.CAET_RestErrorDescription));

            } else

            {

                ar.setResponseCode(response.getHeader(CommonNames.CAET_CingularErrorCode));

                ar.setResponseDescription(response.getHeader(CommonNames.CAET_CingularErrorDescription));

            }

            ar.setFaultCode(response.getHeader(CommonNames.CAET_FaultCode));

            ar.setFaultDescription(response.getHeader(CommonNames.CAET_FaultDesc));

            ar.setFaultLevel(CommonNames.ERROR);

            ar.setFaultEntity(response.getHeader(CommonNames.CAET_FaultEntity));

            ar.setTransactionStatus("E");

            ar.setFaultTimestamp(UtilLib.epochToXmlGC(System.currentTimeMillis()));

            // ar.setFaultTimestamp(UtilLib.epochToXmlGC((new
            // Double(System.nanoTime()/1000000)).longValue()));

            ar.setExternalFaultCode(String.valueOf(httpCode));

            ar.setExternalFaultDescription(CommonErrors.GENERIC_XML_ERROR);

        }

    }

    catch (Exception e) {
        // AuditRecord ar =
        // (AuditRecord)request.getAttribute(CommonNames.AUDIT_RECORD);
        ar.setResponseCode(CommonNames.CSI_GENERIC_UNKNOWN_ERROR);
        ar.setResponseDescription(CommonErrors.DEF_5NN_FAULT_DESC);
        ar.setFaultEntity("CSI");
        ar.setFaultCode(CommonErrors.DEF_5NN_FAULT_CODE);
        ar.setFaultDescription(e.getMessage());
        ar.setFaultLevel("ERROR");
        ar.setFaultSequenceNumber("1");
        ar.setTransactionStatus("E");
        ar.setFaultTimestamp(UtilLib.epochToXmlGC(System.currentTimeMillis()));
        // ar.setFaultTimestamp(UtilLib.epochToXmlGC(((Long)System.nanoTime()/1000000).longValue()));
        logger.error("EXCEPTION - " + e.getMessage());
    }

    finally {
        // AuditRecord ar =
        // (AuditRecord)request.getAttribute(CommonNames.AUDIT_RECORD);
        if (ar != null) {

            if (perfTrackerBean != null && !perfTrackerBean.isAsync()) {
                perfTrackerBean.setAuditRecord(ar);
                logger.debug("Before calling completeLogging");
                completeLogging(request, servicename);
            }

        } else {
            logger.debug("Audit Record is null,abort logging");
        }
    }

}