Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.netcore.hsmart.smsreceiverserver; import com.netcore.hsmart.AppConstants; import com.netcore.hsmart.Utilities; import java.io.IOException; import java.util.concurrent.TimeoutException; import javax.servlet.http.HttpServletRequest; //import javax.servlet.http.HttpServletRequest; import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.rabbitmq.client.Channel; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Date; /** * * @author root */ @Path("publishSms") @Produces(MediaType.APPLICATION_XML) public class PublishSms { private final static String DEFAULT_MSIDIN = "NAN"; private final static String DEFAULT_MSG_TYPE = "NAN"; private final static String DEFAULT_MESSAGE = "NAN"; private final static String DEFAULT_GATEWAY_ID = "NAN"; private final static String DEFAULT_SENDER_ID = "NAN"; private final static String DEFAULT_PRIORITY = "NAN"; //private static Long REF_ID = null; @GET public Response processSmsRequest(@DefaultValue(DEFAULT_MSIDIN) @QueryParam("mobile_number") String msisdn, @DefaultValue(DEFAULT_MSG_TYPE) @QueryParam("message_type") String msgType, @DefaultValue(DEFAULT_MESSAGE) @QueryParam("message") String message, @DefaultValue(DEFAULT_GATEWAY_ID) @QueryParam("gateway_id") String gatewayId, @DefaultValue(DEFAULT_SENDER_ID) @QueryParam("sender_id") String senderId, @DefaultValue(DEFAULT_PRIORITY) @QueryParam("priority") String priority, @Context HttpServletRequest requestContext) throws TimeoutException, IOException { Logger logger = LoggerFactory.getLogger(PublishSms.class); Long REF_ID = null; try { REF_ID = AppConstants.refIdCounter().getAndIncrement(); //adding gateway stats per hour wise AppConstants.getHazelcastClient().getAtomicLong( "PUBSMS_GATEWAY_" + gatewayId + "_" + new SimpleDateFormat("yyyy-MM-dd-HH").format(new Date())) .incrementAndGet(); //logger.info("TOTAL_HITS:"+AppConstants.getHazelcastClient().getAtomicLong("PUBSMS_GATEWAY_"+gatewayId+"_"+new SimpleDateFormat("yyyy-MM-dd-HH").format(new Date())).get()); } catch (Exception e) { logger.error("REF_ID:" + REF_ID + "|HAZELCAST_ERROR:" + e.getMessage()); logger.error( "REF_ID:" + REF_ID + "|ERROR:" + AppConstants.getApplicationCodeMessage("HSMART_PUBSMS_1001")); logger.info("REF_ID:" + REF_ID + "|PUBLISH_STATUS:" + AppConstants.getApplicationCodeMessage("HSMART_GEN_1001")); return Response.status(400).entity( "<?xml version='1.0'?><HTTP><response><status>fail</status><error><code>HSMART_PUBSMS_1001</code><desc>" + AppConstants.getApplicationCodeMessage("HSMART_PUBSMS_1001") + "</desc></error></response></HTTP>") .build(); } logger.info("REF_ID:" + REF_ID + "|QUERY_STRING:" + requestContext.getQueryString()); logger.info("REF_ID:" + REF_ID + "|IP:" + requestContext.getRemoteAddr()); //logger.info("REF_ID:" + REF_ID + "|GAT:" + AppConstants.getGatewayCode(gatewayId)); if ((msisdn == null ? (DEFAULT_MSIDIN != null) : !msisdn.equals(DEFAULT_MSIDIN) && !msisdn.isEmpty()) && (msgType == null ? (DEFAULT_MSG_TYPE != null) : !msgType.equals(DEFAULT_MSG_TYPE) && !msgType.isEmpty()) && (message == null ? (DEFAULT_MESSAGE != null) : !message.equals(DEFAULT_MESSAGE) && !message.isEmpty()) && (gatewayId == null ? (DEFAULT_GATEWAY_ID != null) : !gatewayId.equals(DEFAULT_GATEWAY_ID) && !gatewayId.isEmpty()) && (senderId == null ? (DEFAULT_SENDER_ID != null) : !senderId.equals(DEFAULT_SENDER_ID) && !senderId.isEmpty()) && (priority == null ? (DEFAULT_PRIORITY != null) : !priority.equals(DEFAULT_PRIORITY) && !priority.isEmpty())) { Boolean isValidGateway = Utilities.isValidGateway(gatewayId); if (isValidGateway) { try { final String payload = "ref_id" + AppConstants.getPayloadKVSeparator() + REF_ID + AppConstants.getPayloadGroupSeparator() + "gateway_id" + AppConstants.getPayloadKVSeparator() + gatewayId + AppConstants.getPayloadGroupSeparator() + "message" + AppConstants.getPayloadKVSeparator() + URLEncoder.encode(message, "UTF-8") + AppConstants.getPayloadGroupSeparator() + "msisdn" + AppConstants.getPayloadKVSeparator() + msisdn + AppConstants.getPayloadGroupSeparator() + "sender_id" + AppConstants.getPayloadKVSeparator() + senderId + AppConstants.getPayloadGroupSeparator() + "message_type" + AppConstants.getPayloadKVSeparator() + msgType; logger.info("REF_ID:" + REF_ID + "|PAYLOAD_FOR_MQ:" + payload); Channel channel = AppConstants.getRabbitMqConnection().createChannel(); channel.exchangeDeclare(AppConstants.getSmsReceiverExchangeName(), "direct"); channel.queueDeclare(AppConstants.getSmsReceiverQueuePrefix() + gatewayId, true, false, false, null); channel.queueBind(AppConstants.getSmsReceiverQueuePrefix() + gatewayId, AppConstants.getSmsReceiverExchangeName(), gatewayId); channel.basicPublish(AppConstants.getSmsReceiverExchangeName(), gatewayId, null, payload.getBytes()); channel.close(); //AppConstants.getRabbitMqConnection().close(); logger.info("REF_ID:" + REF_ID + "|PUBLISH_STATUS:" + AppConstants.getApplicationCodeMessage("HSMART_GEN_1000")); return Response.status(200).entity("<?xml version='1.0'?><HTTP><response><requestid>" + REF_ID + "</requestid></response></HTTP>").build(); } catch (NumberFormatException | IOException | TimeoutException e) { logger.error("REF_ID:" + REF_ID + "|EXCEPTION:" + e.getMessage()); logger.error("REF_ID:" + REF_ID + "|ERROR:" + AppConstants.getApplicationCodeMessage("HSMART_PUBSMS_1001")); logger.info("REF_ID:" + REF_ID + "|PUBLISH_STATUS:" + AppConstants.getApplicationCodeMessage("HSMART_GEN_1001")); return Response.status(400).entity( "<?xml version='1.0'?><HTTP><response><status>fail</status><error><code>HSMART_PUBSMS_1001</code><desc>" + AppConstants.getApplicationCodeMessage("HSMART_PUBSMS_1001") + "</desc></error></response></HTTP>") .build(); } } else { logger.error("REF_ID:" + REF_ID + "|ERROR:" + AppConstants.getApplicationCodeMessage("HSMART_PUBSMS_1002")); logger.info("REF_ID:" + REF_ID + "|PUBLISH_STATUS:" + AppConstants.getApplicationCodeMessage("HSMART_GEN_1001")); return Response.status(400).entity( "<?xml version='1.0'?><HTTP><response><status>fail</status><error><code>HSMART_PUBSMS_1002</code><desc>" + AppConstants.getApplicationCodeMessage("HSMART_PUBSMS_1002") + "</desc></error></response></HTTP>") .build(); } } else { logger.error( "REF_ID:" + REF_ID + "|ERROR:" + AppConstants.getApplicationCodeMessage("HSMART_PUBSMS_1003")); logger.info("REF_ID:" + REF_ID + "|PUBLISH_STATUS:" + AppConstants.getApplicationCodeMessage("HSMART_GEN_1001")); return Response.status(400).entity( "<?xml version='1.0'?><HTTP><response><status>fail</status><error><code>HSMART_PUBSMS_1003</code><desc>" + AppConstants.getApplicationCodeMessage("HSMART_PUBSMS_1003") + "</desc></error></response></HTTP>") .build(); } } }