Package servlets

Source Code of servlets.Login

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.*;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.*;

import com.google.gson.Gson;

import communication.Global;


/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/Login")
public class Login extends HttpServlet {
  private static final long serialVersionUID = 1L;
  private static final String CAMINHO_API_LOGIN = "Api/Login/";

  /**
   * @see HttpServlet#HttpServlet()
   */
  public Login() {
    super();
    // TODO Auto-generated constructor stub
  }

  /**
   * Aciona a webapi para requisi��o de login no sistema
   * Documenta��o: http://acshomolog.portaldeassinaturas.com.br/Help/Api/POST-Api-Login-LoginByContextos
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    try {
      Gson gson = new Gson();

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

        HttpSession session = request.getSession(true);
        session.setAttribute(Global.LOGIN, null);

        //Busca o parametro enviado pelo form
        models.Login login = gson.fromJson(request.getParameter("login"), models.Login.class);
        login.addContext(Global.AUTH_CONTEXT_UID); //UID do Portal de Assinaturas
       
        String apiName = CAMINHO_API_LOGIN + "EmailSenha";
        HttpPost httpPost = new HttpPost(Global.ApiPortalBaseURL + apiName);

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

        CloseableHttpClient client = HttpClients.createDefault();

        HttpResponse httpResp = client.execute(httpPost);

        if (httpResp != null) {
          StatusLine statusLine = httpResp.getStatusLine();

          BufferedReader reader = new BufferedReader(new InputStreamReader(httpResp.getEntity().getContent()));
          String data = reader.readLine();

          response.setStatus(statusLine.getStatusCode());

          if (statusLine.getStatusCode() < 300) {
            //Em caso de sucesso, armazena as credencias obtidas           
            LoginResponse loginResp = gson.fromJson(data, LoginResponse.class);

            session.setAttribute(Global.LOGIN, loginResp);
            session.setAttribute(Global.USER_NAME, loginResp.getUserInfo().getNome());
          } else {

            PrintWriter out = response.getWriter();
            out.print(data);
          }
        }
      } else {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
      }

    } catch (Exception e) {
      response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }

  }

}
TOP

Related Classes of servlets.Login

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.