package com.oracle.retail.rib.integration.services.applicationmessageinjector;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.oracle.retail.integration.rib.applicationmessages.v1.*;
import com.retek.rib.binding.exception.InjectorException;
import com.retek.rib.binding.injector.Injector;
import com.retek.rib.binding.injector.InjectorFactory;
import com.retek.rib.domain.payload.PayloadFactory;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.oracle.retail.integration.payload.Payload;
@Stateless
@Path("/injector")
public class ApplicationMessageInjectorResource {
private static Log LOG =
LogFactory.getLog(ApplicationMessageInjectorResource.class);
@GET
@Path("/ping")
@Produces({MediaType.APPLICATION_JSON})
public Response ping(@DefaultValue("hello") @QueryParam("pingMessage") String pingMessage){
String message = "{\"message\": \"Got " + pingMessage + " from server.\"}";
return Response.ok(message, MediaType.APPLICATION_JSON).build();
}
@POST
@Path("/inject")
@Consumes({MediaType.APPLICATION_XML})
public Response injectMessage(ApplicationMessage applicationMessage) throws InjectorException{
verifyNotNull(applicationMessage, "applicationMessage");
invokeInjectForMessageType(applicationMessage.getFamily(), applicationMessage.getType(), applicationMessage.getBusinessObjectId(), applicationMessage.getPayloadXml());
String message = "{\"message\": \"Inject successful.\"}";
return Response.ok(message, MediaType.APPLICATION_JSON).build();
}
private void invokeInjectForMessageType(String family, String messageType, String businessObjectId, String retailPayload)throws InjectorException{
try {
verifyNotNull(family, "family");
verifyNotNull(messageType, "messageType");
verifyNotNull(retailPayload, "retailPayload");
Payload payload = PayloadFactory.unmarshalPayload(family, messageType, retailPayload);
Injector injector = InjectorFactory.getInstance().getInjector(
?? family, messageType);
if (injector == null) {
final String eMsg = "Unknown message"
+ " family/type: " + family + "/" + messageType;
LOG.error(eMsg);
throw new InjectorException(eMsg);
}
if(LOG.isDebugEnabled()){
LOG.debug("Received inject call for family("+family+") type("+messageType+") businessObjectId("+businessObjectId+") with payload:\n" + payload.toString());
}
injector.inject(messageType, businessObjectId, payload);
LOG.debug("Inject call for family("+family+") type("+messageType+") businessObjectId("+businessObjectId+") return.");
?? } catch (InjectorException e) {
final String eMsg = "Exception calling inject.";
LOG.error(eMsg, e);
throw e;
}catch (Exception re) {
final String eMsg = "Exception calling inject.";
LOG.error(eMsg, re);
throw new RuntimeException(eMsg, re);
}
}
private void verifyNotNull(Object field, String fieldName){
if(field == null){
final String eMsg = fieldName + " cannot be null.";
LOG.error(eMsg);
throw new IllegalArgumentException(eMsg);
}
}
}