List of usage examples for javax.servlet.http HttpServletResponse setContentType
public void setContentType(String type);
From source file:com.adnature.framework.util.Struts2Utils.java
/** * ./*w w w .j ava 2 s . c o m*/ * * eg. <br/> * render("text/plain", "hello", "encoding:GBK"); <br/> * render("text/plain", "hello", "no-cache:false"); <br/> * render("text/plain", "hello", "encoding:GBK", "no-cache:false"); * * @param headers * ??header??"encoding:""no-cache:",UTF-8true. */ public static void render(final String contentType, final String content, final String... headers) { try { // ?headers? String encoding = ENCODING_DEFAULT; boolean noCache = NOCACHE_DEFAULT; for (String header : headers) { String headerName = StringUtils.substringBefore(header, ":"); String headerValue = StringUtils.substringAfter(header, ":"); if (StringUtils.equalsIgnoreCase(headerName, ENCODING_PREFIX)) { encoding = headerValue; } else if (StringUtils.equalsIgnoreCase(headerName, NOCACHE_PREFIX)) { noCache = Boolean.parseBoolean(headerValue); } else { throw new IllegalArgumentException(headerName + "??header"); } } HttpServletResponse response = ServletActionContext.getResponse(); // headers? String fullContentType = contentType + ";charset=" + encoding; response.setContentType(fullContentType); if (noCache) { response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); } response.getWriter().write(content); response.getWriter().flush(); } catch (IOException e) { logger.error(e.getMessage(), e); } }
From source file:co.edu.UNal.ArquitecturaDeSoftware.Bienestar.Vista.App.Usuario.Read.java
protected static void leerTaller(HttpServletRequest request, HttpServletResponse response) throws IOException { TallerEntity e = CtrlUsuario.leerTaller(request.getParameter("1")); // id de la taller response.setContentType("application/json;charset=UTF-8"); PrintWriter out = response.getWriter(); if (e.getNombre().isEmpty()) { JSONObject obj = new JSONObject(); obj.put("isError", true); obj.put("errorDescrip", "El taller ya no esta disponible"); out.print(obj);// ww w.j a v a2 s .co m } else { JSONObject obj = new JSONObject(); obj.put("id", e.getIdTaller()); obj.put("nombre", e.getNombre()); obj.put("descripcion", e.getDescripcion()); obj.put("cupos", e.getCupos()); obj.put("costo", e.getCosto()); obj.put("fechaFin", "" + e.getFechaFinRegistro()); obj.put("fechaInicio", "" + e.getFechaInicio()); out.print(obj); } }
From source file:co.edu.UNal.ArquitecturaDeSoftware.Bienestar.Vista.App.Usuario.Read.java
protected static void obtenerInscritosTaller(HttpServletRequest request, HttpServletResponse response) throws IOException { int numC = CtrlUsuario.obtenerInscritosTaller(Integer.parseInt(request.getParameter("1"))); response.setContentType("application/json;charset=UTF-8"); PrintWriter out = response.getWriter(); JSONObject obj = new JSONObject(); obj.put("inscritos", numC); out.print(obj);// ww w . j a va 2s. co m }
From source file:com.wadpam.guja.oauth2.web.Oauth2ClientAuthenticationFilter.java
private static void errorMessage(HttpServletResponse response, int responseCode, String errorMessage) { response.setStatus(responseCode);/*from w ww.ja v a2 s .com*/ try { response.getWriter().write(String.format("{\"error\":\"%s\"}", errorMessage)); response.setContentType(APPLICATION_JSON_WITH_UTF8_CHARSET); } catch (IOException e) { LOGGER.error("Failed to write json body {}", e); } }
From source file:com.vmware.identity.samlservice.Shared.java
/** * Send HTTP response/*from w w w .ja va2s .c om*/ * @param response * @param contentType * @param str * @throws IOException */ public static void sendResponse(HttpServletResponse response, String contentType, String str) throws IOException { response.setContentType(contentType); PrintWriter out = response.getWriter(); out.println(str); out.close(); }
From source file:co.edu.UNal.ArquitecturaDeSoftware.Bienestar.Vista.App.Usuario.Read.java
protected static void leerConvocatoria(HttpServletRequest request, HttpServletResponse response) throws IOException { ConvocatoriaEntity e = CtrlUsuario.leerConvocatoria(request.getParameter("1")); // id de la convocatoria response.setContentType("application/json;charset=UTF-8"); PrintWriter out = response.getWriter(); if (e.getNombre().isEmpty()) { JSONObject obj = new JSONObject(); obj.put("isError", true); obj.put("errorDescrip", "La convocatoria ya no existe"); out.print(obj);// w w w . j a v a2 s. com } else { JSONObject obj = new JSONObject(); obj.put("id", e.getIdConvocatoria()); obj.put("nombre", e.getNombre()); obj.put("descripcion", e.getDescripcion()); obj.put("cupos", e.getCupos()); obj.put("fechaFin", "" + e.getFechaFinRegistro()); out.print(obj); } }
From source file:ch.rasc.downloadchart.DownloadChartServlet.java
private static void handleJpg(HttpServletResponse response, byte[] imageData, Integer width, Integer height, String filename, JpegOptions options) throws IOException { response.setContentType("image/jpeg"); response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + ".jpg\";"); BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageData)); Dimension newDimension = calculateDimension(image, width, height); int imgWidth = image.getWidth(); int imgHeight = image.getHeight(); if (newDimension != null) { imgWidth = newDimension.width;//w w w . ja va2 s.c o m imgHeight = newDimension.height; } BufferedImage newImage = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB); Graphics2D g = newImage.createGraphics(); g.drawImage(image, 0, 0, imgWidth, imgHeight, Color.BLACK, null); g.dispose(); if (newDimension != null) { g.setComposite(AlphaComposite.Src); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); } try (ImageOutputStream ios = ImageIO.createImageOutputStream(response.getOutputStream())) { Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpg"); ImageWriter writer = iter.next(); ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); if (options != null && options.quality != null && options.quality != 0 && options.quality != 100) { iwp.setCompressionQuality(options.quality / 100f); } else { iwp.setCompressionQuality(1); } writer.setOutput(ios); writer.write(null, new IIOImage(newImage, null, null), iwp); writer.dispose(); } }
From source file:co.edu.UNal.ArquitecturaDeSoftware.Bienestar.Vista.App.Admin.CRUDUsuarios.java
protected static void leerUsuariosMultiplesId(HttpServletRequest request, HttpServletResponse response) throws IOException { ArrayList<UsuarioEntity> usuarios = new ArrayList<>(); usuarios = CtrlAdmin.leerMultiplesUsuarios(Integer.parseInt(request.getParameter("1")), Integer.parseInt(request.getParameter("2"))); // id del usuario response.setContentType("application/json;charset=UTF-8"); PrintWriter out = response.getWriter(); JSONArray list1 = new JSONArray(); for (UsuarioEntity usuario : usuarios) { JSONObject obj = new JSONObject(); obj.put("id", usuario.getIdUsuario()); obj.put("titulo", usuario.getNombres() + " " + usuario.getApellidos()); list1.add(obj);//from w w w. j av a2s.c o m } out.print(list1); }
From source file:org.artifactory.util.HttpUtils.java
public static void sendErrorResponse(HttpServletResponse response, int statusCode, String message) throws IOException { response.setContentType(MediaType.APPLICATION_JSON_VALUE); response.setStatus(statusCode);/* ww w.j ava 2s .c om*/ ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); ErrorResponse errorResponse = new ErrorResponse(statusCode, message); response.getWriter().write(mapper.writeValueAsString(errorResponse)); }
From source file:com.vmware.identity.openidconnect.server.Shared.java
public static void writeJSONResponse(HttpServletResponse httpServletResponse, int statusCode, JSONObject jsonObject) throws IOException { Validate.notNull(httpServletResponse, "httpServletResponse"); Validate.notNull(jsonObject, "jsonObject"); PrintWriter writer = null;// w w w . j a v a 2 s. co m try { httpServletResponse.setStatus(statusCode); httpServletResponse.setContentType("application/json"); writer = httpServletResponse.getWriter(); writer.print(jsonObject.toString()); } finally { if (writer != null) { writer.close(); } } }