Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package egpi.tes.ahv.servicio; import egpi.tes.ahv.valueobject.AutenticacionVO; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.PostMethod; import java.io.*; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.*; import javax.servlet.http.*; import net.sf.json.JSONObject; import egpi.tes.ahv.util.ReporteJasperException; /** * * @author AndresVilla */ public class AutenticacionREST extends HttpServlet { Properties properties; HttpClient client = null; JSONObject obj; int status; public void init() throws ServletException { try { cargarPropiedades(); client = new HttpClient(); status = 0; obj = new JSONObject(); } catch (IOException ex) { Logger.getLogger(AutenticacionREST.class.getName()).log(Level.SEVERE, null, ex); } catch (ReporteJasperException ex) { Logger.getLogger(AutenticacionREST.class.getName()).log(Level.SEVERE, null, ex); } } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { HttpSession session = request.getSession(true); session.setAttribute("clientjasper", this.client); session.setAttribute("propiedadesjasper", this.properties); session.removeAttribute("authVO"); autenticacionUsuarioGeneral(); PrintWriter out = response.getWriter(); out.println(obj); } catch (Exception e) { e.printStackTrace(); procesarError(response, e); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { AutenticacionVO authVO = new AutenticacionVO(); authVO.setPassword(request.getParameter("pass")); authVO.setUsername(request.getParameter("user")); autenticacionUsuarioLogin(authVO); HttpSession session = request.getSession(true); session.setAttribute("clientjasper", this.client); session.setAttribute("propiedadesjasper", this.properties); session.setAttribute("authVO", authVO); PrintWriter out = response.getWriter(); out.println(obj); } catch (Exception e) { e.printStackTrace(); procesarError(response, e); } } /** * * @throws IOException */ public void cargarPropiedades() throws IOException, ReporteJasperException { try { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); InputStream inputStream = classLoader.getResourceAsStream("/servidorreportes.properties"); properties = new Properties(); if (inputStream != null) { properties.load(inputStream); } else { throw new ReporteJasperException( "Verifique que exista fichero /classes/servidorreportes.properties "); } } catch (Exception e) { e.printStackTrace(); throw new ReporteJasperException(e.getMessage()); } } /** * * @return */ public void autenticacionUsuarioGeneral() throws ReporteJasperException { try { cargarPropiedades(); if (properties != null) { if (properties.getProperty("urllogueo") != null && properties.getProperty("usuariopublico") != null && properties.getProperty("password") != null) { AutenticacionVO authVO = new AutenticacionVO(); authVO.setServerUrl(properties.getProperty("urllogueo")); authVO.setUsername(properties.getProperty("usuariopublico")); authVO.setPassword(properties.getProperty("password")); procesasLogeoServidor(authVO); } else { throw new ReporteJasperException( "Parametros incompletos verifique fichero servidorreportes.properties "); } } else { throw new ReporteJasperException( "Verifique que exista fichero /classes/servidorreportes.properties "); } } catch (Exception e) { e.printStackTrace(); throw new ReporteJasperException(e.getMessage()); } } /** * * @param authVO * @return */ public void autenticacionUsuarioLogin(AutenticacionVO authVO) throws ReporteJasperException { try { if (properties != null && authVO != null) { if (properties.getProperty("urllogueo") != null && authVO.getUsername() != null && authVO.getPassword() != null) { authVO.setServerUrl(properties.getProperty("urllogueo")); procesasLogeoServidor(authVO); } else { throw new ReporteJasperException( "Parametros incompletos verifique fichero servidorreportes.properties "); } } else { throw new ReporteJasperException( "Verifique que exista fichero /classes/servidorreportes.properties "); } } catch (Exception e) { e.printStackTrace(); throw new ReporteJasperException(e.getMessage()); } } /** * * @param authVO */ public void procesasLogeoServidor(AutenticacionVO authVO) throws ReporteJasperException { try { if (authVO != null) { PostMethod postMethod = new PostMethod(authVO.getServerUrl()); postMethod.addParameter("j_username", authVO.getUsername()); postMethod.addParameter("j_password", authVO.getPassword()); status = client.executeMethod(postMethod); System.out.println("status 00 " + status); if (status == 200) { obj.put("status", String.valueOf(status)); } else { throw new ReporteJasperException(this.properties.getProperty("msg_" + String.valueOf(status))); } } } catch (Exception e) { e.printStackTrace(); throw new ReporteJasperException(e.getMessage()); } } public void procesarError(HttpServletResponse response, Exception e) { System.out.println("\n\nprocesarError=" + e.getMessage() + "\n\n"); try { if (status == 0) { response.sendError(500, e.getMessage()); } else { response.sendError(status, e.getMessage()); } } catch (IOException ex) { Logger.getLogger(AutenticacionREST.class.getName()).log(Level.SEVERE, null, ex); } } }