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.nkapps.billing.webservices; import java.security.MessageDigest; import java.util.Locale; import javax.annotation.Resource; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import javax.servlet.ServletContext; import javax.xml.ws.WebServiceContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.MessageSource; import org.springframework.core.env.Environment; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.nkapps.billing.dao.WSDao; import com.nkapps.billing.models.WsUser; import com.nkapps.billing.services.PaymentService; import com.nkapps.billing.webservices.beans.DoPaymentTransactionArgument; import com.nkapps.billing.webservices.beans.DoPaymentTransactionResult; import com.nkapps.billing.webservices.beans.FinishPaymentTransactionArgument; import com.nkapps.billing.webservices.beans.FinishPaymentTransactionResult; import com.nkapps.billing.webservices.beans.GenericArgument; import com.nkapps.billing.webservices.beans.GenericResult; import com.nkapps.billing.webservices.beans.GetAccountInfoArgument; import com.nkapps.billing.webservices.beans.GetAccountInfoResult; /** * * @author nuraddin */ @WebService(serviceName = "AccountWebService") public class AccountWebService { /** * This is a sample web service operation */ private Logger logger = LoggerFactory.getLogger(AccountWebService.class); @Resource private WebServiceContext context; private Object getBean(String beanName) { ServletContext servletContext = (ServletContext) context.getMessageContext() .get("javax.xml.ws.servlet.context"); WebApplicationContext webApplicationContext = WebApplicationContextUtils .getRequiredWebApplicationContext(servletContext); return webApplicationContext.getAutowireCapableBeanFactory().getBean(beanName); } private String md5Encrypt(String in) throws Exception { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(in.getBytes("UTF-8")); byte byteData[] = md.digest(); //convert the byte to hex format method 1 StringBuffer sb = new StringBuffer(); for (int i = 0; i < byteData.length; i++) { sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1)); } return sb.toString(); } private GenericResult initResult(GenericArgument argument, GenericResult result) { try { String username = argument.getUsername(); String password = md5Encrypt(argument.getPassword()); WSDao wsDao = (WSDao) getBean("wsDao"); WsUser user = wsDao.findByUsernameAndPassword(username, password); if (user == null) { MessageSource messageSource = (MessageSource) getBean("messageSource"); // throw new Exception(messageSource.getMessage("auth.login_or_password_not_correct",null,new Locale("ru"))); Environment environment = (Environment) getBean("environment"); String a = environment.getProperty("ws.allow_user_add"); if ("true".equals(a)) { if (wsDao.createWsUser(username, password) == null) { throw new Exception(messageSource.getMessage("auth.login_or_password_not_correct", null, new Locale("ru"))); } } else { throw new Exception( messageSource.getMessage("auth.login_or_password_not_correct", null, new Locale("ru"))); } } result.setStatus(GenericResult.STATUS_INIT); } catch (Exception e) { logger.error(e.getMessage()); result.setStatus(GenericResult.STATUS_ERROR_AUTH); result.setReason(e.getMessage()); } return result; } @WebMethod(operationName = "hello") public String hello(@WebParam(name = "name") String txt) { return "Hello " + txt + " !"; } @WebMethod(operationName = "GetAccountInfo") public GetAccountInfoResult getAccountInfo(@WebParam(name = "argument") GetAccountInfoArgument argument) { GetAccountInfoResult result = new GetAccountInfoResult(); result = (GetAccountInfoResult) initResult(argument, result); if (result.getStatus() != GenericResult.STATUS_INIT) { return result; } try { PaymentService paymentService = (PaymentService) getBean("paymentService"); result.setAccountInfoBean(paymentService.getAccountInfo(argument.getTin())); result.setStatus(GenericResult.STATUS_SUCCESS); } catch (Exception e) { logger.error(e.getMessage()); result.setStatus(GenericResult.STATUS_ERROR_SYSTEM); result.setReason(e.getMessage()); } return result; } @WebMethod(operationName = "DoPaymentTransaction") public DoPaymentTransactionResult doPaymentTransaction( @WebParam(name = "argument") DoPaymentTransactionArgument argument) { DoPaymentTransactionResult result = new DoPaymentTransactionResult(); result = (DoPaymentTransactionResult) initResult(argument, result); if (result.getStatus() != GenericResult.STATUS_INIT) { return result; } try { PaymentService paymentService = (PaymentService) getBean("paymentService"); result.setTransaction(paymentService.getTransaction(argument.getTin(), argument.getSerialNumber())); result.setStatus(GenericResult.STATUS_SUCCESS); } catch (Exception e) { logger.error(e.getMessage()); result.setStatus(GenericResult.STATUS_ERROR_SYSTEM); result.setReason(e.getMessage()); } return result; } @WebMethod(operationName = "FinishPaymentTransaction") public FinishPaymentTransactionResult finishPaymentTransaction( @WebParam(name = "argument") FinishPaymentTransactionArgument argument) { FinishPaymentTransactionResult result = new FinishPaymentTransactionResult(); result = (FinishPaymentTransactionResult) initResult(argument, result); if (result.getStatus() != GenericResult.STATUS_INIT) { return result; } try { PaymentService paymentService = (PaymentService) getBean("paymentService"); paymentService.finishTransaction(argument.getTransaction()); result.setStatus(GenericResult.STATUS_SUCCESS); } catch (Exception e) { logger.error(e.getMessage()); result.setStatus(GenericResult.STATUS_ERROR_SYSTEM); result.setReason(e.getMessage()); } return result; } }