acromusashi.stream.component.json.camel.JsonExtractor.java Source code

Java tutorial

Introduction

Here is the source code for acromusashi.stream.component.json.camel.JsonExtractor.java

Source

/**
* Copyright (c) Acroquest Technology Co, Ltd. All Rights Reserved.
* Please read the associated COPYRIGHTS file for more details.
*
* THE SOFTWARE IS PROVIDED BY Acroquest Technolog Co., Ltd.,
* 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 HOLDER BE LIABLE FOR ANY
* CLAIM, DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
package acromusashi.stream.component.json.camel;

import java.io.IOException;
import java.net.URLDecoder;
import java.text.MessageFormat;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import org.apache.camel.Exchange;
import org.apache.camel.converter.stream.InputStreamCache;
import org.apache.storm.shade.org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Exchange???HTTP Post Resuest?????Value?<br>
 * HTTP Post Resuest???1?Key(?)-Value???Value?<br>
 * ?isDoValidate????JSON????JSON?????
 * 
 * @author kimura
 */
public class JsonExtractor {
    /** logger */
    private static final Logger logger = LoggerFactory.getLogger(JsonExtractor.class);

    /** Key-Value? */
    private static final String KEYVALUE_SEPARATOR = "=";

    /** ? */
    public boolean isDoValidate;

    /**
     * ?????
     */
    public JsonExtractor() {
    }

    /**
     * Exchange?Value?<br>
     * Value???????????<br>
     * isDoValidate==true???JSON???????????
     * 
     * @param exchange ?Exchange
     */
    public void extractJson(Exchange exchange) {
        InputStreamCache iscache = exchange.getIn().getBody(InputStreamCache.class);

        String postStr = null;

        try {
            postStr = IOUtils.toString(iscache);
            postStr = URLDecoder.decode(postStr, "utf-8");
        } catch (IOException ioex) {
            logger.warn("Failed to extract string from Request. Dispose request.", ioex);
            return;
        }

        int firstSeparatorIndex = postStr.indexOf(KEYVALUE_SEPARATOR);
        if (firstSeparatorIndex == -1) {
            String logFormat = "Received request is invalid. Dispose request. : Request={0}";
            String logMessage = MessageFormat.format(logFormat, postStr);
            logger.warn(logMessage);
            return;
        }

        String jsonStr = postStr.substring(firstSeparatorIndex + 1);

        if (this.isDoValidate == false) {
            exchange.getOut().setBody(jsonStr);
            return;
        }

        JSONObject jsonObj = null;

        try {
            jsonObj = JSONObject.fromObject(jsonStr);
        } catch (JSONException jsonex) {
            logger.warn("Received request is invalid. Dispose request.", jsonex);
            return;
        }

        exchange.getOut().setBody(jsonObj.toString());
    }
}