com.gtcgroup.jped.rest.helper.JprRequestUtilHelper.java Source code

Java tutorial

Introduction

Here is the source code for com.gtcgroup.jped.rest.helper.JprRequestUtilHelper.java

Source

/*
 * [Licensed per the Open Source "MIT License".]
 *
 * Copyright (c) 2006 - 2016 by
 * Global Technology Consulting Group, Inc. at
 * http://gtcGroup.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.gtcgroup.jped.rest.helper;

import javax.ws.rs.client.ClientRequestContext;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.gtcgroup.jped.core.helper.JpcLoggingCacheHelper;
import com.gtcgroup.jped.core.po.JpcBasicExceptionPO;
import com.gtcgroup.jped.rest.exception.JprConvertException;

/**
 * This Util Helper class provides convenience methods for handling
 * {@link ClientRequestContext} instances.
 *
 * <p style="font-family:Verdana; font-size:10px; font-style:italic">
 * Copyright (c) 2006 - 2016 by Global Technology Consulting Group, Inc. at
 * <a href="http://gtcGroup.com">gtcGroup.com </a>.
 * </p>
 *
 * @author Marvin Toll
 * @since v3.0
 */
public class JprRequestUtilHelper {

    private static final String CLASS_NAME = JprRequestUtilHelper.class.getSimpleName();

    /**
     * @param requestContext
     */
    public static void filterIncomingJsonRequest(final ClientRequestContext requestContext) {

        final String methodName = "filterIncomingRequest";

        final StringBuilder message = new StringBuilder();
        message.append("JAX-RS ");
        message.append(requestContext.getMethod());
        message.append(": ");
        message.append(requestContext.getUri().toString());

        if (null != requestContext.getEntity()) {

            message.append("\n");
            final ObjectMapper mapper = new ObjectMapper();
            mapper.enable(SerializationFeature.INDENT_OUTPUT);

            try {
                message.append(
                        mapper.writerWithDefaultPrettyPrinter().writeValueAsString(requestContext.getEntity()));

            } catch (final Exception e) {
                throw new JprConvertException(
                        new JpcBasicExceptionPO().setClassName(JprRequestUtilHelper.CLASS_NAME)
                                .setMessage("Unable to log incoming request."),
                        e);
            }
        }

        JpcLoggingCacheHelper.getLogger().logModeLifecycle(JprRequestUtilHelper.CLASS_NAME, methodName,
                message.toString());

        return;
    }
}