Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package th.in.oneauthen.api;
- import java.util.Date;
- 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 org.apache.commons.lang3.StringUtils;
- import org.bouncycastle.util.encoders.Base64;
- import org.jboss.logging.Logger;
- import th.in.oneauth.servlet.LoginServlet;
- import th.in.oneauth.servlet.SigningServlet;
- import th.in.oneauthen.api.object.SigningRequest;
- import th.in.oneauthen.api.object.SigningResponse;
- import th.in.oneauthen.object.DocumentDB;
- import th.in.oneauthen.object.SignatureProfileDB;
- import th.in.oneauthen.object.SystemConfigDB;
- import th.in.oneauthen.object.UserUidDB;
- import th.in.oneauthen.object.DAO.DocumentDAO;
- import th.in.oneauthen.object.DAO.SignatureProfileDAO;
- import th.in.oneauthen.object.DAO.SystemConfigDAO;
- import th.in.oneauthen.object.DAO.UserUidDAO;
- import th.in.oneauthen.signing.SignatureProfile;
- import th.in.oneauthen.signing.SignatureUtil;
- @Path("/service")
- public class SigningService{
- @POST
- @Path("/signing")
- @Consumes("application/json")
- @Produces("application/json")
- public SigningResponse signing(SigningRequest request) {
- SigningResponse response = new SigningResponse();
- String errMsg = "System error, please contact administrator";
- response.setResponseCode(-1);
- response.setResponseMessage(errMsg);
- try {
- // ============= Request validation ================
- // SignatureProfileDB sigProfile = new SignatureProfileDB().findByAccessToken();
- UserUidDB user = UserUidDAO.findUserByAccessToken(request.getAccessToken());
- if (user==null) {
- response.setResponseCode(401);
- response.setResponseMessage("Authorization denined. Invalid access token.");
- return response;
- }
- SignatureProfileDB sigProfile = new SignatureProfileDAO().find(request.getProfileId()); // For testing
- if (sigProfile==null) {
- response.setResponseCode(411);
- response.setResponseMessage("Invalid profile Id.");
- return response;
- }
- if (sigProfile.getUserUid().getUserId()!=user.getUserId()) {
- response.setResponseCode(403);
- response.setResponseMessage("Authorization denined. User not authorized to signature profile.");
- return response;
- }
- SignatureProfile profile = new SignatureProfile(sigProfile);
- byte[] pdfData = Base64.decode(request.getPdfData());
- String timestampURL = SigningServlet.TIMESTAMP_URL;
- try {
- SystemConfigDB sysConfig = new SystemConfigDAO().find(SigningServlet.SYSTEM_PARAM_TIMESTAMP);
- if (sysConfig != null)
- timestampURL = sysConfig.getValue();
- } catch (Exception e) {
- e.printStackTrace();
- }
- SignatureUtil util = new SignatureUtil(profile, timestampURL);
- byte[] signPDF = util.SignTheDocument(pdfData);
- if (signPDF!=null && signPDF.length>0) {
- response.setResponseCode(0);
- response.setResponseMessage("PDF signing complete.");
- response.setPdfData(new String(Base64.encode(signPDF)));
- UserUidDB userID = new UserUidDAO().find(user.getUserId());
- DocumentDAO sessionLogDao = new DocumentDAO();
- DocumentDB sessionLog = new DocumentDB();
- sessionLog.setCreator(userID);
- sessionLog.setDocFile(signPDF);
- sessionLog.setRemainingSigner(1);
- sessionLog.settimeSign(new Date());
- sessionLog.setFileName(null);
- sessionLog.setTypeApi("ApiResful");
- sessionLog.setTotalDocument(1);
- sessionLog.setStatus("success");
- sessionLogDao.save(sessionLog);
- }else {
- UserUidDB userID = new UserUidDAO().find(user.getUserId());
- DocumentDAO sessionLogDao = new DocumentDAO();
- DocumentDB sessionLog = new DocumentDB();
- sessionLog.setCreator(userID);
- sessionLog.setDocFile(signPDF);
- sessionLog.setRemainingSigner(1);
- sessionLog.settimeSign(new Date());
- sessionLog.setFileName(null);
- sessionLog.setTypeApi("ApiResful");
- sessionLog.setTotalDocument(1);
- sessionLog.setStatus("fail");
- sessionLogDao.save(sessionLog);
- }
- } catch ( Exception e ) {
- Logger logger = Logger.getLogger(SigningService.class);
- logger.error("API request error. "+e.getMessage());
- if (!StringUtils.isEmpty(e.getMessage())) {
- errMsg = errMsg.replace("System error", e.getMessage());
- response.setResponseMessage(errMsg);
- }
- }
- return response;
- }
- @GET
- @Path("/restdummy")
- @Produces("application/json")
- public SigningResponse dummy() {
- SigningResponse response = new SigningResponse();
- response.setResponseCode(200);
- response.setResponseMessage("PDF Signing Complete");
- response.setPdfData(Base64.toBase64String("PDF Signing Complete".getBytes()));
- return response;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement