/** * Writes the query results to the HTTP response output stream. * * @param resp * @param writer * @param request * @param response * @param updateCountsFor * @throws IOException */ protected final void stringResponse( ServiceResponse resp, Writer writer, HttpServletRequest request, HttpServletResponse response, Set<LogEvent> updateCountsFor) throws IOException { if (resp instanceof ErrorResponse) { errorResponse( ((ErrorResponse) resp).getStatus(), ((ErrorResponse) resp).toString(), request, response, updateCountsFor); } else if (resp.isEmpty()) { log.warn( "stringResponse: I got an empty ServiceResponce " + "(must be already converted to the ErrorResponse)"); errorResponse(NO_RESULTS_FOUND, "no results found", request, response, updateCountsFor); } else { response.setContentType("text/plain"); DataResponse dresp = (DataResponse) resp; log.debug("QUERY RETURNED " + dresp.getData().toString().length() + " chars"); // take care to count provider's data accessed events Set<String> providers = dresp.getProviders(); updateCountsFor.addAll(LogEvent.fromProviders(providers)); // log to the db (for analysis and reporting) // problems with logging subsystem should not fail the entire service try { service.log(updateCountsFor, clientIpAddress(request)); } catch (Throwable ex) { log.error("LogUtils.log failed", ex); } if (dresp.getData() instanceof Path) { File resultFile = ((Path) dresp.getData()).toFile(); // this is some temp. file response.setHeader("Content-Length", String.valueOf(resultFile.length())); FileReader reader = new FileReader(resultFile); IOUtils.copyLarge(reader, writer); response.flushBuffer(); reader.close(); resultFile.delete(); } else { writer.write(dresp.getData().toString()); response.flushBuffer(); } } }
/** * Http error response from the error bean. * * @param status * @param detailedMsg * @param request * @param response * @param updateCountsFor * @throws IOException */ protected final void errorResponse( Status status, String detailedMsg, HttpServletRequest request, HttpServletResponse response, Set<LogEvent> updateCountsFor) throws IOException { if (updateCountsFor == null) updateCountsFor = new HashSet<LogEvent>(); // to count the error (code), also add - updateCountsFor.add(LogEvent.from(status)); // problems with logging subsystem should not fail the entire service try { service.log(updateCountsFor, clientIpAddress(request)); } catch (Throwable ex) { log.error("LogUtils.log failed", ex); } response.sendError(status.getErrorCode(), status.getErrorMsg() + "; " + detailedMsg); }