Package servlets

Source Code of servlets.Assinatura

package servlets;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import models.LoginResponse;

import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import communication.Global;

/**
* Servlet implementation class Assinatura
*/
@WebServlet("/Assinatura")
public class Assinatura extends HttpServlet {
  private static final long serialVersionUID = 1L;
  private static final String CAMINHO_API_ASSINATURA = "api/assinatura/";
      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Assinatura() {
        super();
        // TODO Auto-generated constructor stub
    }

  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {

      if (request.getParameter("action") != null) {

        HttpSession session = request.getSession(true);
        LoginResponse login = (LoginResponse) session.getAttribute(Global.LOGIN);

        if (login != null) {

          //Busca o parametro enviado pelo form
          String action = request.getParameter("action").toLowerCase();
          HttpResponse httpResp = null;
         
          switch (action) {
          case "iniciar":
            if (request.getParameter("signStart") != null) {
              httpResp = iniciar(request.getParameter("signStart"), login);
            } else {
              response.sendError(HttpServletResponse.SC_BAD_REQUEST);
            }
           
            break;

          case "finalizar":
            if (request.getParameter("signFinish") != null) {
              httpResp = finalizar(request.getParameter("signFinish"), login);
            } else {
              response.sendError(HttpServletResponse.SC_BAD_REQUEST);
            }

            break;
          default:
            break;
          }
         
          if (httpResp != null){
            StatusLine statusLine = httpResp.getStatusLine();

            BufferedReader reader = new BufferedReader(new InputStreamReader(httpResp.getEntity().getContent()));
            String data = reader.readLine();
           
            response.setContentType("application/json");

            response.setStatus(statusLine.getStatusCode());
            PrintWriter out = response.getWriter();
            out.print(data);
          }
        } else {
          response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
        }

      } else {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
      }

    } catch (Exception e) {
      response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
  }
 
  /*
   * Inicia o processo de assinatura de um documento.
   * http://homolog.portaldeassinaturas.com.br/Help/Api/POST-Api-Assinatura-Iniciar
   */
  private HttpResponse iniciar(String signStart, LoginResponse login) throws Exception {

    try {
     
      String apiName = CAMINHO_API_ASSINATURA + "iniciar";

      HttpPost httpPost = new HttpPost(Global.ApiPortalBaseURL + apiName);

      httpPost.setHeader(Global.ACS_AUTH_CONTEXT_UID, login.getUidContextos(0));
      httpPost.setHeader(Global.ACS_AUTH_TOKEN, login.getToken());
      httpPost.setHeader(Global.ACS_AUTH_USER_UID, login.getUserInfo().getUid());

      StringEntity entity = new StringEntity(signStart, StandardCharsets.UTF_8);
      entity.setContentType("application/json");
      httpPost.setEntity(entity);

      CloseableHttpClient client = HttpClients.createDefault();

      HttpResponse httpResp = client.execute(httpPost);
     
      return httpResp;

    } catch (Exception e) {
      throw e;
    }
 
  }

  /*
   * Finaliza a assinatura de um documento
   * http://homolog.portaldeassinaturas.com.br/Help/Api/POST-Api-Assinatura-Finalizar
   */
  private HttpResponse finalizar(String signFinish, LoginResponse login) throws Exception {

    try {
     
      String apiName = CAMINHO_API_ASSINATURA + "finalizar";

      HttpPost httpPost = new HttpPost(Global.ApiPortalBaseURL + apiName);

      httpPost.setHeader(Global.ACS_AUTH_CONTEXT_UID, login.getUidContextos(0));
      httpPost.setHeader(Global.ACS_AUTH_TOKEN, login.getToken());
      httpPost.setHeader(Global.ACS_AUTH_USER_UID, login.getUserInfo().getUid());
     
      StringEntity entity = new StringEntity(signFinish, StandardCharsets.UTF_8);
      entity.setContentType("application/json");
      httpPost.setEntity(entity);

      CloseableHttpClient client = HttpClients.createDefault();

      HttpResponse httpResp = client.execute(httpPost);
     
      return httpResp;

    } catch (Exception e) {
      throw e;
    }
  }
// 

}
TOP

Related Classes of servlets.Assinatura

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.