Advertisement
ballchaichana

signservice

Sep 8th, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.67 KB | None | 0 0
  1. package th.in.oneauthen.api;
  2.  
  3. import java.util.Date;
  4.  
  5. import javax.ws.rs.Consumes;
  6. import javax.ws.rs.GET;
  7. import javax.ws.rs.POST;
  8. import javax.ws.rs.Path;
  9. import javax.ws.rs.Produces;
  10.  
  11. import org.apache.commons.lang3.StringUtils;
  12. import org.bouncycastle.util.encoders.Base64;
  13. import org.jboss.logging.Logger;
  14.  
  15. import th.in.oneauth.servlet.LoginServlet;
  16. import th.in.oneauth.servlet.SigningServlet;
  17. import th.in.oneauthen.api.object.SigningRequest;
  18. import th.in.oneauthen.api.object.SigningResponse;
  19. import th.in.oneauthen.object.DocumentDB;
  20. import th.in.oneauthen.object.SignatureProfileDB;
  21. import th.in.oneauthen.object.SystemConfigDB;
  22. import th.in.oneauthen.object.UserUidDB;
  23. import th.in.oneauthen.object.DAO.DocumentDAO;
  24. import th.in.oneauthen.object.DAO.SignatureProfileDAO;
  25. import th.in.oneauthen.object.DAO.SystemConfigDAO;
  26. import th.in.oneauthen.object.DAO.UserUidDAO;
  27. import th.in.oneauthen.signing.SignatureProfile;
  28. import th.in.oneauthen.signing.SignatureUtil;
  29.  
  30. @Path("/service")
  31. public class SigningService{
  32.  
  33.     @POST
  34.     @Path("/signing")
  35.     @Consumes("application/json")
  36.     @Produces("application/json")
  37.     public SigningResponse signing(SigningRequest request) {
  38.         SigningResponse response = new SigningResponse();
  39.         String errMsg = "System error, please contact administrator";
  40.         response.setResponseCode(-1);
  41.         response.setResponseMessage(errMsg);
  42.         try {
  43.             // ============= Request validation ================
  44.             // SignatureProfileDB sigProfile = new SignatureProfileDB().findByAccessToken();
  45.             UserUidDB user = UserUidDAO.findUserByAccessToken(request.getAccessToken());
  46.             if (user==null) {
  47.                 response.setResponseCode(401);
  48.                 response.setResponseMessage("Authorization denined. Invalid access token.");
  49.                 return response;
  50.             }
  51.            
  52.             SignatureProfileDB sigProfile = new SignatureProfileDAO().find(request.getProfileId()); // For testing
  53.             if (sigProfile==null) {
  54.                 response.setResponseCode(411);
  55.                 response.setResponseMessage("Invalid profile Id.");
  56.                 return response;
  57.             }
  58.            
  59.             if (sigProfile.getUserUid().getUserId()!=user.getUserId()) {
  60.                 response.setResponseCode(403);
  61.                 response.setResponseMessage("Authorization denined. User not authorized to signature profile.");
  62.                 return response;
  63.             }
  64.            
  65.             SignatureProfile profile = new SignatureProfile(sigProfile);
  66.            
  67.             byte[] pdfData = Base64.decode(request.getPdfData());
  68.             String timestampURL = SigningServlet.TIMESTAMP_URL;
  69.             try {
  70.                 SystemConfigDB sysConfig = new SystemConfigDAO().find(SigningServlet.SYSTEM_PARAM_TIMESTAMP);
  71.                 if (sysConfig != null)
  72.                     timestampURL = sysConfig.getValue();
  73.             } catch (Exception e) {
  74.                 e.printStackTrace();
  75.             }
  76.            
  77.             SignatureUtil util = new SignatureUtil(profile, timestampURL);
  78.             byte[] signPDF = util.SignTheDocument(pdfData);
  79.            
  80.             if (signPDF!=null && signPDF.length>0) {
  81.                 response.setResponseCode(0);
  82.                 response.setResponseMessage("PDF signing complete.");
  83.                 response.setPdfData(new String(Base64.encode(signPDF)));
  84.                
  85.                 UserUidDB userID  = new UserUidDAO().find(user.getUserId());
  86.                 DocumentDAO sessionLogDao = new DocumentDAO();
  87.                 DocumentDB sessionLog = new DocumentDB();
  88.                 sessionLog.setCreator(userID);
  89.                 sessionLog.setDocFile(signPDF);
  90.                 sessionLog.setRemainingSigner(1);
  91.                 sessionLog.settimeSign(new Date());
  92.                 sessionLog.setFileName(null);
  93.                 sessionLog.setTypeApi("ApiResful");
  94.                 sessionLog.setTotalDocument(1);
  95.                 sessionLog.setStatus("success");
  96.  
  97.                 sessionLogDao.save(sessionLog);
  98.             }else {
  99.                 UserUidDB userID  = new UserUidDAO().find(user.getUserId());
  100.                 DocumentDAO sessionLogDao = new DocumentDAO();
  101.                 DocumentDB sessionLog = new DocumentDB();
  102.                 sessionLog.setCreator(userID);
  103.                 sessionLog.setDocFile(signPDF);
  104.                 sessionLog.setRemainingSigner(1);
  105.                 sessionLog.settimeSign(new Date());
  106.                 sessionLog.setFileName(null);
  107.                 sessionLog.setTypeApi("ApiResful");
  108.                 sessionLog.setTotalDocument(1);
  109.                 sessionLog.setStatus("fail");
  110.  
  111.                 sessionLogDao.save(sessionLog);
  112.             }
  113.         } catch ( Exception e ) {
  114.             Logger logger = Logger.getLogger(SigningService.class);
  115.             logger.error("API request error. "+e.getMessage());
  116.            
  117.             if (!StringUtils.isEmpty(e.getMessage())) {
  118.                 errMsg = errMsg.replace("System error", e.getMessage());
  119.                 response.setResponseMessage(errMsg);
  120.             }
  121.         }
  122.         return response;
  123.     }
  124.    
  125.     @GET
  126.     @Path("/restdummy")
  127.     @Produces("application/json")
  128.     public SigningResponse dummy() {
  129.         SigningResponse response = new SigningResponse();
  130.         response.setResponseCode(200);
  131.         response.setResponseMessage("PDF Signing Complete");
  132.         response.setPdfData(Base64.toBase64String("PDF Signing Complete".getBytes()));
  133.         return response;
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement