List of usage examples for java.io PrintWriter write
public void write(String s)
From source file:au.org.paperminer.main.UserFilter.java
/** * Return user prefs as JSON, checking that the user id in the cookie (if any) still exists. * @param req The request/* w ww . j a v a 2 s . c om*/ * @param resp The response */ private void getPrefs(HttpServletRequest req, HttpServletResponse resp) { m_logger.debug("get user prefs "); try { String id = CookieHelper.getCookieValue(req, PaperMinerConstants.PMC_USER_ID); UserHelper helper = new UserHelper(id); if (!helper.isKnownUser()) { removeCookie(resp); req.setAttribute(PaperMinerConstants.ERROR_PAGE, "e103"); } else { HashMap<String, String> map = new HashMap<String, String>(); map.put("id", CookieHelper.getCookieValue(req, PaperMinerConstants.PMC_USER_ID)); map.put("key", CookieHelper.getCookieValue(req, PaperMinerConstants.PMC_TROVE_KEY)); map.put("em", CookieHelper.getCookieValue(req, PaperMinerConstants.PMC_USER_STATUS)); map.put("stat", CookieHelper.getCookieValue(req, PaperMinerConstants.PMC_USER_STATUS)); String jsonStr = JSONValue.toJSONString(map); resp.setContentType("text/json"); PrintWriter pm = resp.getWriter(); pm.write(jsonStr); } } catch (IOException ex) { req.setAttribute(PaperMinerConstants.ERROR_PAGE, "e110"); } }
From source file:au.org.paperminer.main.UserFilter.java
/** * Returns requesting user's stored query data as JSON array of associative arrays using DB table columns as keys. * @param req/* www.j av a 2 s .c om*/ * @param resp */ private void getQueryData(HttpServletRequest req, HttpServletResponse resp) { UserHelper helper = getUserHelper(req); if (helper == null) { req.setAttribute(PaperMinerConstants.ERROR_PAGE, "e104"); } else { try { m_logger.debug("Fetching queries for " + CookieHelper.getCookieValue(req, PaperMinerConstants.PMC_USER_ID)); ArrayList<HashMap<String, String>> list = helper.getSavedQueries(); m_logger.debug("Fetchied " + list.size()); m_logger.debug("stored query count = " + list.size()); String jsonStr = JSONValue.toJSONString(list); m_logger.debug("query string = " + jsonStr); resp.setContentType("text/json"); PrintWriter pm = resp.getWriter(); pm.write(jsonStr); pm.close(); } catch (IOException ex) { req.setAttribute(PaperMinerConstants.ERROR_PAGE, "e114"); } catch (PaperMinerException ex) { req.setAttribute(PaperMinerConstants.ERROR_PAGE, "e114"); } } }
From source file:com.suntek.gztpb.controller.VehicleLicenseController.java
@RequestMapping(value = "saveApply.htm", method = RequestMethod.POST) public String saveApply(VehicleLicenseModel apply, HttpServletRequest request, HttpServletResponse response) throws ServiceException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); try {/* ww w . j a v a 2 s . c o m*/ String picPath = this.servletContext.getRealPath("/picUpload/") + "\\"; String applyNum = IdGenerator.getInstance().getBizCode("ITMS_SEQ", 5); apply.setApplyNum(applyNum); byte[] fileStream = CommonUtil.getFileByte(picPath + apply.getVehicleImage()); vehicleLicenseService.saveApply(apply, fileStream); out.write("<script>parent.saveCallback(1,'" + applyNum + "')</script>"); } catch (Exception e) { logger.error(e.getMessage()); out.write("<script>parent.saveCallback(0)</script>"); } return null; }
From source file:nl.minbzk.dwr.zoeken.enricher.uploader.SolrResultUploader.java
/** * {@inheritDoc}// w w w . j a v a2s .co m */ @Override public void writeOut(final String databaseName, final PrintWriter writer) { SolrServer server = solrServers.get(databaseName); if (server != null && server instanceof CloudSolrServer) writer.write(((CloudSolrServer) server).getZkStateReader().getClusterState().toString()); else if (server != null && server instanceof HttpSolrServer) try { writer.write(((HttpSolrServer) server).ping().toString()); } catch (SolrServerException e) { writer.write(String.format("Unable to ping Solr server for database %s: %s", databaseName, e.getMessage())); } catch (IOException e) { writer.write(String.format("Unable to ping Solr server for database %s: %s", databaseName, e.getMessage())); } else writer.write(String.format("Unknown or empty Solr server for database %s", databaseName)); }
From source file:au.org.paperminer.main.LocationFilter.java
/** * Returns JSON struct for a specific location id, or blank. * @param req//w ww. j av a 2 s . c o m * @param resp */ private void getGSDetails(HttpServletRequest req, HttpServletResponse resp) { HashMap<String, HashMap<String, String>> map = new HashMap<String, HashMap<String, String>>(); try { String arg = req.getParameter("lst"); if ((arg != null) && (arg.length() > 0)) { String[] ids = arg.split(","); m_logger.debug("locationFilter getGSDetails: " + arg + " length:" + ids.length); for (int i = 0; i < ids.length; i++) { HashMap<String, String> tmp = m_helper.getLocationInfo(ids[i]); map.put(ids[i], tmp); m_logger.debug(" getGSDetails fetched: " + tmp.get("name") + " (" + ids[i] + ")"); } } resp.setContentType("text/json"); PrintWriter pm = resp.getWriter(); String jsonStr = ""; if (map.size() > 0) { jsonStr = "{\"locns\":" + JSONValue.toJSONString(map) + "}"; } pm.write(jsonStr); pm.close(); } catch (PaperMinerException ex) { req.setAttribute(PaperMinerConstants.ERROR_PAGE, "e302"); } catch (IOException ex) { req.setAttribute(PaperMinerConstants.ERROR_PAGE, "e114"); } }
From source file:au.org.paperminer.main.LocationFilter.java
/** * Returns JSON struct for a given set of TROVE ids: * refs: [{troveId:[locnId,freq]*]*//from w w w .j ava 2 s . c om * or blank if none. * @param req * @param resp */ private void getReferences(HttpServletRequest req, HttpServletResponse resp) { HashMap<String, ArrayList<ArrayList<String>>> map = new HashMap<String, ArrayList<ArrayList<String>>>(); try { String arg = req.getParameter("lst"); if ((arg != null) && (arg.length() > 0)) { String[] refs = arg.split(","); m_logger.debug("locationFilter getReferences: " + arg + " length:" + refs.length); for (int i = 0; i < refs.length; i++) { ArrayList<ArrayList<String>> tmp = m_helper.getLocationsForRef(refs[i]); if (tmp != null) { map.put(refs[i], tmp); m_logger.debug("locationFilter ref: " + refs[i] + " is " + tmp); } } } resp.setContentType("text/json"); PrintWriter pm = resp.getWriter(); String jsonStr = ""; if (map.size() > 0) { jsonStr = "{\"refs\":" + JSONValue.toJSONString(map) + "}"; } pm.write(jsonStr); pm.close(); m_logger.debug("locationFilter getReferences JSON: " + jsonStr); } catch (PaperMinerException ex) { req.setAttribute(PaperMinerConstants.ERROR_PAGE, "e301"); } catch (IOException ex) { req.setAttribute(PaperMinerConstants.ERROR_PAGE, "e114"); } }
From source file:com.hp.avmon.home.web.HomeController.java
@RequestMapping(value = "/getRegCode") public void getRegCode(HttpServletRequest request, PrintWriter writer) { Map map = new HashMap(); try {//from w w w. j a v a 2s . c o m String regCode = licenseService.getServerCpuIdFromFile(); map.put("regCode", regCode); } catch (Exception e) { logger.error("Exception: {}", e); } String json = JackJson.fromObjectToJson(map); writer.write(json); writer.flush(); writer.close(); }
From source file:com.test.servlet.AppErrorHandler.java
private void processError(HttpServletRequest request, HttpServletResponse response) throws IOException { // Analyze the servlet exception Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception"); Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code"); String servletName = (String) request.getAttribute("javax.servlet.error.servlet_name"); if (servletName == null) { servletName = "Unknown"; }//www . ja v a 2s . co m String requestUri = (String) request.getAttribute("javax.servlet.error.request_uri"); if (requestUri == null) { requestUri = "Unknown"; } // Set response content type response.setContentType("application/json"); PrintWriter out = response.getWriter(); JSONObject object = new JSONObject(); object.put("success", -1); JSONObject error = new JSONObject(); error.put("status_code", statusCode); error.put("requested_uri", requestUri); error.put("servlet_name", servletName); error.put("exception_name", throwable.getClass().getName()); error.put("exception_message", throwable.getMessage()); object.put("error", error); out.write(object.toString()); }
From source file:controllers.ServerController.java
public void deletePrinter(HttpServletRequest request, HttpServletResponse response) throws IOException, InterruptedException { PrintWriter out = response.getWriter(); String pName = request.getParameter("pName"); if (!threadsList.contains(pName)) { DeletePrinterThread dpt = new DeletePrinterThread(request, pName); dpt.start();//w w w. jav a 2s. c o m threadsList.put(dpt.getName(), dpt); } out.write(pName); out.close(); }
From source file:gsn.http.FieldUpload.java
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String msg;/*from w w w . j a v a 2 s .c om*/ Integer code; PrintWriter out = res.getWriter(); ArrayList<String> paramNames = new ArrayList<String>(); ArrayList<String> paramValues = new ArrayList<String>(); //Check that we have a file upload request boolean isMultipart = ServletFileUpload.isMultipartContent(req); if (!isMultipart) { out.write("not multipart!"); code = 666; msg = "Error post data is not multipart!"; logger.error(msg); } else { // Create a factory for disk-based file items FileItemFactory factory = new DiskFileItemFactory(); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Set overall request size constraint upload.setSizeMax(5 * 1024 * 1024); List items; try { // Parse the request items = upload.parseRequest(req); //building xml data out of the input String cmd = ""; String vsname = ""; Base64 b64 = new Base64(); StringBuilder sb = new StringBuilder("<input>\n"); Iterator iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (item.getFieldName().equals("vsname")) { //define which cmd block is sent sb.append("<vsname>" + item.getString() + "</vsname>\n"); vsname = item.getString(); } else if (item.getFieldName().equals("cmd")) { //define which cmd block is sent cmd = item.getString(); sb.append("<command>" + item.getString() + "</command>\n"); sb.append("<fields>\n"); } else if (item.getFieldName().split(";")[0].equals(cmd)) { //only for the defined cmd sb.append("<field>\n"); sb.append("<name>" + item.getFieldName().split(";")[1] + "</name>\n"); paramNames.add(item.getFieldName().split(";")[1]); if (item.isFormField()) { sb.append("<value>" + item.getString() + "</value>\n"); paramValues.add(item.getString()); } else { sb.append("<value>" + new String(b64.encode(item.get())) + "</value>\n"); paramValues.add(new String(b64.encode(item.get()))); } sb.append("</field>\n"); } } sb.append("</fields>\n"); sb.append("</input>\n"); //do something with xml aka statement.toString() AbstractVirtualSensor vs = null; try { vs = Mappings.getVSensorInstanceByVSName(vsname).borrowVS(); vs.dataFromWeb(cmd, paramNames.toArray(new String[] {}), paramValues.toArray(new Serializable[] {})); } catch (VirtualSensorInitializationFailedException e) { logger.warn("Sending data back to the source virtual sensor failed !: " + e.getMessage(), e); } finally { Mappings.getVSensorInstanceByVSName(vsname).returnVS(vs); } code = 200; msg = "The upload to the virtual sensor went successfully! (" + vsname + ")"; } catch (ServletFileUpload.SizeLimitExceededException e) { code = 600; msg = "Upload size exceeds maximum limit!"; logger.error(msg, e); } catch (Exception e) { code = 500; msg = "Internal Error: " + e; logger.error(msg, e); } } //callback to the javascript out.write("<script>window.parent.GSN.msgcallback('" + msg + "'," + code + ");</script>"); }