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.proceso.ProcesarXML; import egpi.tes.ahv.proceso.SincronizarMenu; import egpi.tes.ahv.util.ReporteJasperException; import egpi.tes.ahv.valueobject.AutenticacionVO; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; /** * * @author AndresVilla */ public class MenuReporteJasper extends HttpServlet { HttpClient client; Properties properties; SincronizarMenu sincronizarMenu; ProcesarXML listadoReportes; ArrayList listaReporte; AutenticacionVO autenticacionVO; JSONObject obj; int status; String pathaplicacion; public void init() throws ServletException { sincronizarMenu = new SincronizarMenu(); listadoReportes = new ProcesarXML(); listaReporte = new ArrayList(); autenticacionVO = null; status = 0; obj = new JSONObject(); } /** * * @param request * @param response * @throws ServletException * @throws IOException */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { String separador = System.getProperty("file.separator"); HttpSession session = request.getSession(true); client = new HttpClient(); pathaplicacion = "pathpublico"; properties = (Properties) session.getAttribute("propiedadesjasper"); if (request.getParameter("inputcontrol") != null && request.getParameter("inputcontrol").equals("1")) { System.out.println("VERIFICANDO LOS PARAMETROS DE PROCESO" + client); AutenticacionVO autenticacionVO = null; if (session.getAttribute("authVO") == null) { autenticacionVO = new AutenticacionVO(); autenticacionVO.setUsername(this.properties.getProperty("usuariopublico")); autenticacionVO.setPassword(this.properties.getProperty("password")); } else { autenticacionVO = (AutenticacionVO) session.getAttribute("authVO"); } if (validarUsuario(autenticacionVO)) { System.out.println("USUARIO VALIDADO CORRECTAMENTE...."); procesarControlesEntrada(request.getParameter("parentFolder")); } } else { sincronizarMenu.setPath(request.getRealPath("") + separador + properties.getProperty("pathinstalacion") + separador + "menu" + separador + "cache"); if (request.getParameter("cerrar") != null && request.getParameter("cerrar").equals("1")) { session.removeAttribute("authVO"); autenticacionVO = null; } obtenerDescriptorGeneral(); session.setAttribute("menuDeplegable", sincronizarMenu.menuDesplegable()); } PrintWriter out = response.getWriter(); out.println(obj); } catch (Exception e) { e.printStackTrace(); procesarError(response, e); } } /** * * @param request * @param response * @throws ServletException * @throws IOException */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { System.out.println("\n\ndoPost\n\n"); String separador = System.getProperty("file.separator"); System.out.println("getRealPath:" + request.getRealPath("") + separador + "menu" + separador + "cache"); HttpSession session = request.getSession(true); client = (HttpClient) session.getAttribute("clientjasper"); properties = (Properties) session.getAttribute("propiedadesjasper"); autenticacionVO = (AutenticacionVO) session.getAttribute("authVO"); sincronizarMenu.setPath(request.getRealPath("") + separador + properties.getProperty("pathinstalacion") + separador + "menu" + separador + "cache"); obtenerDescriptorGeneral(); session.setAttribute("menuDeplegable", sincronizarMenu.menuDesplegable()); PrintWriter out = response.getWriter(); out.println(obj); } catch (Exception e) { e.printStackTrace(); procesarError(response, e); } } /** * */ public void obtenerDescriptorGeneral() throws ReporteJasperException { boolean privado = false; try { String user = autenticacionVO != null && autenticacionVO.getUsername() != null ? autenticacionVO.getUsername() : this.properties.getProperty("usuariopublico"); String pass = autenticacionVO != null && autenticacionVO.getPassword() != null ? autenticacionVO.getPassword() : this.properties.getProperty("password"); if (this.properties != null && user != null && this.properties.getProperty("usuariopublico") != null && !this.properties.getProperty("usuariopublico").equals(user)) { pathaplicacion = "pathprivado"; privado = true; } else { pathaplicacion = "pathpublico"; } System.out.println("pathaplicacion==" + pathaplicacion + " privado=" + privado); PostMethod postMethod = new PostMethod(properties.getProperty("urllogueo")); postMethod.addParameter("j_username", user); postMethod.addParameter("j_password", pass); status = client.executeMethod(postMethod); System.out.println("status 01=" + status + "pathaplicacion:" + pathaplicacion); if (status == 200) { String resourseURL = properties.getProperty("urlrecursos") + "" + properties.getProperty(pathaplicacion); GetMethod getMethod = new GetMethod(resourseURL); getMethod.setQueryString("?type=reportUnit&recursive=1"); status = client.executeMethod(getMethod); if (status == 200) { String descriptorSource = getMethod.getResponseBodyAsString(); listaReporte = (ArrayList) this.listadoReportes.getReportes(descriptorSource); sincronizarMenu.setProperties(properties); sincronizarMenu.setUSER(user); sincronizarMenu.setPASS(pass); sincronizarMenu.sincronizarArbolNavegacion(listaReporte); obj.put("status", String.valueOf(status)); obj.put("user", String.valueOf(user)); obj.put("privado", String.valueOf(privado)); } else { throw new ReporteJasperException(this.properties.getProperty("msg_" + String.valueOf(status))); } } else { throw new ReporteJasperException(this.properties.getProperty("msg_" + String.valueOf(status))); } } catch (IOException ex) { ex.printStackTrace(); throw new ReporteJasperException(ex.getMessage()); } } public boolean validarUsuario(AutenticacionVO authVO) throws ReporteJasperException { try { boolean resul = false; authVO.setServerUrl(properties.getProperty("urllogueo")); 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) { resul = true; } else { throw new ReporteJasperException(this.properties.getProperty("msg_" + String.valueOf(status))); } return resul; } catch (IOException ex) { throw new ReporteJasperException(ex.getMessage()); } } public void procesarControlesEntrada(String reportName) throws ReporteJasperException { try { String resourseURL = properties.getProperty("urllogueo") + "rest_v2/reports" + reportName + "/inputControls/"; System.out.println("resourseURL=" + resourseURL); GetMethod getMethod = new GetMethod(resourseURL); getMethod.setRequestHeader("accept", "application/json"); status = client.executeMethod(getMethod); if (status == 200) { String descriptorSource = getMethod.getResponseBodyAsString(); System.out.println("" + descriptorSource); obj.put("status", status); obj.put("object", descriptorSource); } else { throw new ReporteJasperException(this.properties.getProperty("msg_" + String.valueOf(status))); } } catch (Exception e) { 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); } } }