Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package th.in.oneauth.servlet;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.MultipartConfig;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.Part;
- import org.apache.commons.io.IOUtils;
- import th.in.oneauthen.object.SignatureProfileDB;
- import th.in.oneauthen.object.UserUidDB;
- import th.in.oneauthen.object.DAO.SignatureProfileDAO;
- import th.in.oneauthen.signing.SignatureProfile;;
- @WebServlet("/uploadKey")
- @MultipartConfig
- public class UploadKeyServlet extends HttpServlet {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- // public static final String SESSION_PARAM_USER = "userSession";
- public static final String URL_DASHBORD = "dashboard.jsp";
- public static final String SYSTEM_SIGNATURE_PROFILE = "keyname";
- public static final String KEY_PIN = "password";
- public static final String SIGING_IMG = "img";
- public static String SESSION_IMG_KEY = "";
- public static String SESSION_KEY = "";
- public static String PATH_KEY = "C:\\userKey\\key\\";
- public static String PATH_IMG_SIGN = "C:\\userKey\\img_sign\\";
- public static String PARAM_ACTION = "action";
- public static final String PARAM_ACTION_UPLOAD = "upload";
- public static final String PARAM_ACTION_EDIT = "edit";
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- //edit old profile
- String oldProfileName = request.getParameter("oldprofileName");
- // user_uid
- UserUidDB userID = (UserUidDB) request.getSession().getAttribute(LoginServlet.SESSION_PARAM_USER);
- int user_id = userID.getUserId();
- // profileName
- String profileName = request.getParameter("profileName"); // Retrieves <input type="text" name="description">
- // password or key_pin
- String password = request.getParameter("key_pin");
- // save KEY
- Part filePart = request.getPart("file_key"); // Retrieves <input type="file" name="file">
- String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // MSIE fix.
- InputStream fileContent = filePart.getInputStream();
- byte[] bytes_Key = IOUtils.toByteArray(fileContent);
- // request.getSession().setAttribute("key_PDF", bytes_Key);
- String full_PATH = PATH_KEY + fileName;
- try {
- writeBytesToFileNio(bytes_Key, full_PATH);
- // String save_KEY_PATH = saveTo(fileContent, full_PATH);
- // request.getSession().setAttribute(SESSION_KEY, save_KEY_PATH);
- } catch (Exception e) {
- e.printStackTrace();
- LogoutServlet.doLogout(request, response, "System error !! Can not save KEY in srver");
- }
- String save_KEY_PATH = (String) request.getSession().getAttribute(SESSION_KEY);
- // save IMG
- Part filePart_img = request.getPart("file_img");
- String fileName_img = Paths.get(filePart_img.getSubmittedFileName()).getFileName().toString();
- InputStream fileContent_img = filePart_img.getInputStream();
- byte[] bytes_Key_img = IOUtils.toByteArray(fileContent_img);
- String full_PATH_img = PATH_IMG_SIGN + fileName_img;
- try {
- writeBytesToFileNio(bytes_Key_img, full_PATH_img);
- // String save_IMG_PATH = saveTo(fileContent_img, full_PATH_img);
- // request.getSession().setAttribute(SESSION_IMG_KEY, save_IMG_PATH);
- } catch (Exception e) {
- e.printStackTrace();
- LogoutServlet.doLogout(request, response, "System error !! Can not save IMG in server");
- }
- String save_IMG_PATH = (String) request.getSession().getAttribute(SESSION_IMG_KEY);
- // send to check at
- try {
- SignatureProfile sessionUploadKey = null;
- sessionUploadKey.generateNewProfile(profileName, bytes_Key, password, user_id);
- } catch (Exception e) {
- e.printStackTrace();
- LogoutServlet.doLogout(request, response, "GenerateNewProfile Error !" + e.getMessage());
- }
- PARAM_ACTION = "upload";
- switch (PARAM_ACTION) {
- case PARAM_ACTION_UPLOAD:
- SignatureProfileDAO profileDAO = new SignatureProfileDAO();
- SignatureProfileDB profileDB = new SignatureProfileDB();
- SignatureProfileDB profileCheck = new SignatureProfileDAO().findByNameAndUserUID(profileName, user_id);
- if(profileCheck!=null) {
- LogoutServlet.doLogout(request, response, "Error can not Save same profilename");
- }
- else {
- profileDB.setProfileName(profileName);
- profileDB.setProfileKey(full_PATH);
- profileDB.setSigImg(full_PATH_img);
- profileDB.setProfileKeyPIN(password);
- profileDB.setSigDisplayPage(1);
- profileDB.setUserUid(userID);
- try {
- profileDAO.save(profileDB);
- System.out.println("Save Key success!!");
- request.getRequestDispatcher(URL_DASHBORD).forward(request, response);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- LogoutServlet.doLogout(request, response, "Error can not Save Key in DB!!" + e.getMessage());
- }
- }
- break;
- case PARAM_ACTION_EDIT:
- SignatureProfileDB editSigProfile = new SignatureProfileDAO().findByNameAndUserUID("chaichana555", user_id);//(oldProfileName,user_id);
- SignatureProfileDB checkProfile = new SignatureProfileDAO().findByNameAndUserUID(profileName, user_id);
- if(checkProfile==null) {
- editSigProfile.setProfileName(profileName);
- editSigProfile.setProfileKey(full_PATH);
- editSigProfile.setProfileKeyPIN(password);
- editSigProfile.setSigImg(full_PATH_img);
- new SignatureProfileDAO().updateSignatureProfileEdit(editSigProfile);
- System.out.println("Edit profile success!!");
- request.getRequestDispatcher(URL_DASHBORD).forward(request, response);
- }else {
- LogoutServlet.doLogout(request, response, "Error can not Edit same profilename");
- }
- break;
- }
- }
- private static void writeBytesToFileNio(byte[] bFile, String fileDest) {
- try {
- Path path = Paths.get(fileDest);
- Files.write(path, bFile);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement