public static String showSecurity(HttpServletRequest req, String role) { StringBuilder sbuff = new StringBuilder(); sbuff.append("Security Info\n"); sbuff.append(" req.getRemoteUser(): ").append(req.getRemoteUser()).append("\n"); sbuff.append(" req.getUserPrincipal(): ").append(req.getUserPrincipal()).append("\n"); sbuff .append(" req.isUserInRole(") .append(role) .append("):") .append(req.isUserInRole(role)) .append("\n"); sbuff.append(" ------------------\n"); return sbuff.toString(); }
/** * Show details about the request * * @param servlet used to get teh servlet context, may be null * @param req the request * @return string showing the details of the request. */ public static String showRequestDetail(HttpServlet servlet, HttpServletRequest req) { StringBuilder sbuff = new StringBuilder(); sbuff.append("Request Info\n"); sbuff.append(" req.getServerName(): ").append(req.getServerName()).append("\n"); sbuff.append(" req.getServerPort(): ").append(req.getServerPort()).append("\n"); sbuff.append(" req.getContextPath:").append(req.getContextPath()).append("\n"); sbuff.append(" req.getServletPath:").append(req.getServletPath()).append("\n"); sbuff.append(" req.getPathInfo:").append(req.getPathInfo()).append("\n"); sbuff.append(" req.getQueryString:").append(req.getQueryString()).append("\n"); sbuff .append(" getQueryStringDecoded:") .append(EscapeStrings.urlDecode(req.getQueryString())) .append("\n"); /*try { sbuff.append(" getQueryStringDecoded:").append(URLDecoder.decode(req.getQueryString(), "UTF-8")).append("\n"); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); }*/ sbuff.append(" req.getRequestURI:").append(req.getRequestURI()).append("\n"); sbuff.append(" getRequestBase:").append(getRequestBase(req)).append("\n"); sbuff.append(" getRequestServer:").append(getRequestServer(req)).append("\n"); sbuff.append(" getRequest:").append(getRequest(req)).append("\n"); sbuff.append("\n"); sbuff.append(" req.getPathTranslated:").append(req.getPathTranslated()).append("\n"); String path = req.getPathTranslated(); if ((path != null) && (servlet != null)) { ServletContext context = servlet.getServletContext(); sbuff.append(" getMimeType:").append(context.getMimeType(path)).append("\n"); } sbuff.append("\n"); sbuff.append(" req.getScheme:").append(req.getScheme()).append("\n"); sbuff.append(" req.getProtocol:").append(req.getProtocol()).append("\n"); sbuff.append(" req.getMethod:").append(req.getMethod()).append("\n"); sbuff.append("\n"); sbuff.append(" req.getContentType:").append(req.getContentType()).append("\n"); sbuff.append(" req.getContentLength:").append(req.getContentLength()).append("\n"); sbuff.append(" req.getRemoteAddr():").append(req.getRemoteAddr()); try { sbuff .append(" getRemoteHost():") .append(java.net.InetAddress.getByName(req.getRemoteHost()).getHostName()) .append("\n"); } catch (java.net.UnknownHostException e) { sbuff.append(" getRemoteHost():").append(e.getMessage()).append("\n"); } sbuff.append(" getRemoteUser():").append(req.getRemoteUser()).append("\n"); sbuff.append("\n"); sbuff.append("Request Parameters:\n"); Enumeration params = req.getParameterNames(); while (params.hasMoreElements()) { String name = (String) params.nextElement(); String values[] = req.getParameterValues(name); if (values != null) { for (int i = 0; i < values.length; i++) { sbuff .append(" ") .append(name) .append(" (") .append(i) .append("): ") .append(values[i]) .append("\n"); } } } sbuff.append("\n"); sbuff.append("Request Headers:\n"); Enumeration names = req.getHeaderNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); Enumeration values = req.getHeaders(name); // support multiple values if (values != null) { while (values.hasMoreElements()) { String value = (String) values.nextElement(); sbuff.append(" ").append(name).append(": ").append(value).append("\n"); } } } sbuff.append(" ------------------\n"); return sbuff.toString(); }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // set the response Shepherd myShepherd = new Shepherd(); Vector rEncounters = new Vector(); // setup data dir String rootWebappPath = getServletContext().getRealPath("/"); File webappsDir = new File(rootWebappPath).getParentFile(); File shepherdDataDir = new File(webappsDir, CommonConfiguration.getDataDirectoryName()); // if(!shepherdDataDir.exists()){shepherdDataDir.mkdir();} File encountersDir = new File(shepherdDataDir.getAbsolutePath() + "/encounters"); // if(!encountersDir.exists()){encountersDir.mkdir();} // set up the files String gisFilename = "geneGIS_export_" + request.getRemoteUser() + ".csv"; File gisFile = new File(encountersDir.getAbsolutePath() + "/" + gisFilename); myShepherd.beginDBTransaction(); try { // set up the output stream FileOutputStream fos = new FileOutputStream(gisFile); OutputStreamWriter outp = new OutputStreamWriter(fos); try { EncounterQueryResult queryResult = EncounterQueryProcessor.processQuery( myShepherd, request, "year descending, month descending, day descending"); rEncounters = queryResult.getResult(); int numMatchingEncounters = rEncounters.size(); // build the CSV file header StringBuffer locusString = new StringBuffer(""); int numLoci = 2; // most covered species will be loci try { numLoci = (new Integer(CommonConfiguration.getProperty("numLoci"))).intValue(); } catch (Exception e) { System.out.println("numPloids configuration value did not resolve to an integer."); e.printStackTrace(); } for (int j = 0; j < numLoci; j++) { locusString.append(",Locus" + (j + 1) + " A1,Locus" + (j + 1) + " A2"); } // out.println("<html><body>"); // out.println("Individual ID,Other ID 1,Date,Time,Latitude,Longitude,Area,Sub // Area,Sex,Haplotype"+locusString.toString()); outp.write( "Individual ID,Other ID 1,Date,Time,Latitude,Longitude,Area,Sub Area,Sex,Haplotype" + locusString.toString() + "\n"); for (int i = 0; i < numMatchingEncounters; i++) { Encounter enc = (Encounter) rEncounters.get(i); String assembledString = ""; if (enc.getIndividualID() != null) { assembledString += enc.getIndividualID(); } if (enc.getAlternateID() != null) { assembledString += "," + enc.getAlternateID(); } else { assembledString += ","; } String dateString = ","; if (enc.getYear() > 0) { dateString += enc.getYear(); if (enc.getMonth() > 0) { dateString += ("-" + enc.getMonth()); if (enc.getDay() > 0) { dateString += ("-" + enc.getDay()); } } } assembledString += dateString; String timeString = ","; if (enc.getHour() > -1) { timeString += enc.getHour() + ":" + enc.getMinutes(); } assembledString += timeString; if ((enc.getDecimalLatitude() != null) && (enc.getDecimalLongitude() != null)) { assembledString += "," + enc.getDecimalLatitude(); assembledString += "," + enc.getDecimalLongitude(); } else { assembledString += ",,"; } assembledString += "," + enc.getVerbatimLocality(); assembledString += "," + enc.getLocationID(); assembledString += "," + enc.getSex(); // find and print the haplotype String haplotypeString = ","; if (enc.getHaplotype() != null) { haplotypeString += enc.getHaplotype(); } // find and print the ms markers String msMarkerString = ""; List<TissueSample> samples = enc.getTissueSamples(); int numSamples = samples.size(); boolean foundMsMarkers = false; for (int k = 0; k < numSamples; k++) { if (!foundMsMarkers) { TissueSample t = samples.get(k); List<GeneticAnalysis> analyses = t.getGeneticAnalyses(); int aSize = analyses.size(); for (int l = 0; l < aSize; l++) { GeneticAnalysis ga = analyses.get(l); if (ga.getAnalysisType().equals("MicrosatelliteMarkers")) { foundMsMarkers = true; MicrosatelliteMarkersAnalysis ga2 = (MicrosatelliteMarkersAnalysis) ga; List<Locus> loci = ga2.getLoci(); int localLoci = loci.size(); for (int m = 0; m < localLoci; m++) { Locus locus = loci.get(m); if (locus.getAllele0() != null) { msMarkerString += "," + locus.getAllele0(); } else { msMarkerString += ","; } if (locus.getAllele1() != null) { msMarkerString += "," + locus.getAllele1(); } else { msMarkerString += ","; } } } } } } // out.println("<p>"+assembledString+haplotypeString+msMarkerString+"</p>"); outp.write(assembledString + haplotypeString + msMarkerString + "\n"); } outp.close(); outp = null; // now write out the file response.setContentType("text/csv"); response.setHeader("Content-Disposition", "attachment;filename=" + gisFilename); ServletContext ctx = getServletContext(); // InputStream is = ctx.getResourceAsStream("/encounters/"+gisFilename); InputStream is = new FileInputStream(gisFile); int read = 0; byte[] bytes = new byte[BYTES_DOWNLOAD]; OutputStream os = response.getOutputStream(); while ((read = is.read(bytes)) != -1) { os.write(bytes, 0, read); } os.flush(); os.close(); } catch (Exception ioe) { ioe.printStackTrace(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(ServletUtilities.getHeader(request)); out.println( "<html><body><p><strong>Error encountered</strong> with file writing. Check the relevant log.</p>"); out.println( "<p>Please let the webmaster know you encountered an error at: EncounterSearchExportGeneGISFormat servlet</p></body></html>"); out.println(ServletUtilities.getFooter()); out.close(); outp.close(); outp = null; } } catch (Exception e) { e.printStackTrace(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(ServletUtilities.getHeader(request)); out.println("<html><body><p><strong>Error encountered</strong></p>"); out.println( "<p>Please let the webmaster know you encountered an error at: EncounterSearchExportGeneGISFormat servlet</p></body></html>"); out.println(ServletUtilities.getFooter()); out.close(); } myShepherd.rollbackDBTransaction(); myShepherd.closeDBTransaction(); }