if(traceLog != null){
traceLog.error("Exception occured while reading JSON inpu as string");
traceLog.info("Error detail: Lines read successfully: " + jsonBuffer.toString());
traceLog.info("Error detail: Exception message" + e.getMessage());
}
throw new JSONFault("Client",jsonBuffer.toString(),e.getMessage(),null,e);
}finally{
try {
if(traceLog != null)
traceLog.info("JSON input reader closed");
input.close();
bufferReader.close();
} catch (Exception e) {}
}
/*
* Step 3: If request method in post and operation name located in query string, then append
* operation name part of query string.
*
* Refer getQueryStringInputStream() "else" condition in case of GET method
*/
if(queryString != null && !queryString.isEmpty() && !isJSONGetRequest()){
if(traceLog != null)
traceLog.info("POST method with query string added. Opeartion name identified from first parameter");
String params[] = queryString.split("&");
// http post request: Operation name should be first parameter if query string present.
if(params.length > 0 && params[0].indexOf('=') == -1){
jsonBuffer.insert(0, ("{\"" + params[0] +"\":"));
jsonBuffer.append("}");
}
}
/*
* Step 4: Parsing input JSON string into java Object.
*/
if(traceLog != null)
traceLog.info("Parsing input JSON string into java Object");
Object inputJSON = new JSONReader().read(jsonBuffer.toString());
/*
* Step 5: If input json parsed successfully by JSON reader and is it is map the process as operation.
*/
if(inputJSON != null && inputJSON instanceof Map){
if(traceLog != null)
traceLog.info("Input JSON parsed successfully as plain map");
/********************************************************************************/
/* ACTUAL JSON TO JAVA CONVERSION HAPPEN HERE */
/********************************************************************************/
Map<String, Object> requestJSONMap = (Map<String, Object>) inputJSON;
/*
* Step 5.1 check is it comming from json service, with status flag.
*/
if(JSONCodec.STATUS_PROPERTY_NAME != null && requestJSONMap.containsKey(JSONCodec.STATUS_PROPERTY_NAME)){
if(traceLog != null)
traceLog.info("Input identified as direct output of jsonservice since it has " + JSONCodec.STATUS_PROPERTY_NAME);
// Output of JSON service and its status is false, then create fault message.
if(!new Boolean(requestJSONMap.get(JSONCodec.STATUS_PROPERTY_NAME).toString())){
return new com.sun.xml.ws.message.FaultMessage(Messages.createEmpty(this.codec.getSoapVersion()),null);
}
// Remove codec set value WARN user should not used codec specific key
requestJSONMap.remove(JSONCodec.STATUS_PROPERTY_NAME);
}
if(traceLog != null)
traceLog.info("Reading operation using service endpoint definition");
/*
* Step 5.2: Remove out bound property from JSON to avoid it serializing in json.
*/
if(requestJSONMap.containsKey(MessageContext.MESSAGE_OUTBOUND_PROPERTY)){
requestJSONMap.remove(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
}
/*
* Step 5.3: Only one operation should be handle.
*/
if(requestJSONMap.size() == 1){
// Must be only one operation
Entry<String, Object> operation = requestJSONMap.entrySet().iterator().next();
if(traceLog != null){
traceLog.info("Operaion identified as . " + operation.getKey());
}
this.packet.invocationProperties.put(JSONCodec.JSON_MAP_KEY, operation.getValue());
try {
return new MessageBodyBuilder(this.codec).handleMessage(this.packet,operation.getKey());
} catch (Exception e) {
throw new JSONFault("Client","Failed to create message body."+e.getMessage(),"MessageBody Builder",null,e);
}
} else if(traceLog != null){
traceLog.error("Operaion unknown in this endpoint. Please read endpoint documentation page for list of operations.");
}
if(traceLog != null)
traceLog.add("ERROR-JSON-DECODER" , "Unknown payload/operation in json request");
throw new JSONFault("Client","Unknown payload/operation in json request","Codec",null);
}else{
if(traceLog != null) traceLog.error("Input JSON parsing failed. parse result : " + String.valueOf(jsonBuffer));
throw new JSONFault("Client","Invalid JSON input : " + jsonBuffer.toString(),"Codec",null);
}
}