Example usage for javax.servlet.http HttpServletResponse setContentType

List of usage examples for javax.servlet.http HttpServletResponse setContentType

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse setContentType.

Prototype


public void setContentType(String type);

Source Link

Document

Sets the content type of the response being sent to the client, if the response has not been committed yet.

Usage

From source file:org.duracloud.duradmin.util.SpaceUtil.java

public static void streamToResponse(InputStream is, HttpServletResponse response, String mimetype,
        String contentLength) throws ContentStoreException, IOException {

    OutputStream outStream = response.getOutputStream();

    try {// w w w  .j ava 2 s .  c  om
        response.setContentType(mimetype);

        if (contentLength != null) {
            response.setContentLengthLong(Long.parseLong(contentLength));
        }
        byte[] buf = new byte[1024];
        int read = -1;
        while ((read = is.read(buf)) > 0) {
            outStream.write(buf, 0, read);
        }

        response.flushBuffer();
    } catch (Exception ex) {
        if (ex.getCause() instanceof ContentStateException) {
            response.reset();
            response.setStatus(HttpStatus.SC_CONFLICT);
            String message = "The requested content item is currently in long-term storage"
                    + " with limited retrieval capability. Please contact "
                    + "DuraCloud support (https://wiki.duraspace.org/x/6gPNAQ) "
                    + "for assistance in retrieving this content item.";
            //It is necessary to pad the message in order to force Internet Explorer to
            //display the server sent text rather than display the browser default error message.
            //If the message is less than 512 bytes, the browser will ignore the message.
            //c.f. http://support.microsoft.com/kb/294807
            message += StringUtils.repeat(" ", 512);
            outStream.write(message.getBytes());
        } else {
            throw ex;
        }
    } finally {
        try {
            outStream.close();
        } catch (Exception e) {
            log.warn("failed to close outputstream ( " + outStream + "): message=" + e.getMessage(), e);
        }
    }
}

From source file:nl.dtls.fairdatapoint.api.controller.utils.HttpHeadersUtils.java

/**
 * Set response header for the internal server errors
 * //from  w  w  w. j a  v a 2 s. co  m
 * @param response  Http response
 * @param ex    Server exception
 * @return returns null (as a response body)
 */
public static String set500ResponseHeaders(HttpServletResponse response, Exception ex) {
    String errorMessage = ("Internal server error; Error message : " + ex.getMessage());
    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    try {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, errorMessage);
    } catch (IOException ex1) {
        LOGGER.warn(
                "Error setting error message for internal server " + "error; The error : " + ex1.getMessage());
    }
    response.setContentType(MediaType.TEXT_PLAIN_VALUE);
    return null;
}

From source file:co.edu.UNal.ArquitecturaDeSoftware.Bienestar.Vista.App.Admin.CUDEventos.java

protected static void consultarUsuariosEnTallerId(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    ArrayList<UsuarioEntity> usuarios = new ArrayList<>();
    usuarios = CtrlAdmin.obtenerUsuariosEnTaller(Integer.parseInt(request.getParameter("3")),
            Integer.parseInt(request.getParameter("2")), Integer.parseInt(request.getParameter("1")));

    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);// ww w  .  j  a va  2  s . co  m
    }
    out.print(list1);
}

From source file:co.edu.UNal.ArquitecturaDeSoftware.Bienestar.Vista.App.Admin.CUDEventos.java

protected static void consultarDocentesEnTallerId(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    ArrayList<UsuarioEntity> usuarios = new ArrayList<>();
    usuarios = CtrlAdmin.obtenerUsuariosEnTaller(Integer.parseInt(request.getParameter("1")),
            Integer.parseInt(request.getParameter("2")), Integer.parseInt(request.getParameter("3")));

    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);/*  w w  w .j a v a2s.  co m*/
    }
    out.print(list1);
}

From source file:ch.ralscha.extdirectspring.util.ExtDirectSpringUtil.java

/**
 * Checks etag and sends back HTTP status 304 if not modified. If modified sets
 * content type and content length, adds cache headers (
 * {@link #addCacheHeaders(HttpServletResponse, String, Integer)}), writes the data
 * into the {@link HttpServletResponse#getOutputStream()} and flushes it.
 *
 * @param request the HTTP servlet request
 * @param response the HTTP servlet response
 * @param data the response data/*from ww  w.  j  ava 2  s  . c  om*/
 * @param contentType the content type of the data (i.e.
 * "application/javascript;charset=UTF-8")
 * @throws IOException
 */
public static void handleCacheableResponse(HttpServletRequest request, HttpServletResponse response,
        byte[] data, String contentType) throws IOException {
    String ifNoneMatch = request.getHeader("If-None-Match");
    String etag = "\"0" + DigestUtils.md5DigestAsHex(data) + "\"";

    if (etag.equals(ifNoneMatch)) {
        response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return;
    }

    response.setContentType(contentType);
    response.setContentLength(data.length);

    addCacheHeaders(response, etag, 6);

    @SuppressWarnings("resource")
    ServletOutputStream out = response.getOutputStream();
    out.write(data);
    out.flush();
}

From source file:co.edu.UNal.ArquitecturaDeSoftware.Bienestar.Vista.App.Usuario.Read.java

protected static void quitarDocenteTaller(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    ArrayList r = CtrlUsuario.abandonarTallerDocente(Integer.parseInt(request.getParameter("1")),
            Integer.parseInt(request.getParameter("2"))); //1. idUsuario, 2. idEvento

    response.setContentType("application/json;charset=UTF-8");
    PrintWriter out = response.getWriter();
    if (r.get(0) == "error") {
        JSONObject obj = new JSONObject();
        obj.put("isError", true);
        obj.put("errorDescrip", r.get(1));
        out.print(obj);/*w  ww.j  a  v  a2s  . c o m*/
    } else if (r.get(0) == "isExitoso") {
        JSONObject obj = new JSONObject();
        obj.put("Exitoso", true);
        out.print(obj);
    } else {
        Util.errordeRespuesta(r, out);
    }
}

From source file:co.edu.UNal.ArquitecturaDeSoftware.Bienestar.Vista.App.Usuario.Read.java

protected static void quitarUsuarioTaller(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    ArrayList r = CtrlUsuario.abandonarTallerUsuario(Integer.parseInt(request.getParameter("1")),
            Integer.parseInt(request.getParameter("2"))); //1. idUsuario, 2. idEvento

    response.setContentType("application/json;charset=UTF-8");
    PrintWriter out = response.getWriter();
    if (r.get(0) == "error") {
        JSONObject obj = new JSONObject();
        obj.put("isError", true);
        obj.put("errorDescrip", r.get(1));
        out.print(obj);//w w  w.  j  a  v  a 2s  . c  om
    } else if (r.get(0) == "isExitoso") {
        JSONObject obj = new JSONObject();
        obj.put("Exitoso", true);
        out.print(obj);
    } else {
        Util.errordeRespuesta(r, out);
    }
}

From source file:co.edu.UNal.ArquitecturaDeSoftware.Bienestar.Vista.App.Admin.CUDEventos.java

protected static void crearConvocatoria(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    ArrayList r = CtrlAdmin.crearConvocatoria(request.getParameter("1"), // nombre
            request.getParameter("2"), // descripcin
            request.getParameter("4"), // fin
            Integer.parseInt(request.getParameter("6")) // cupos
    );// w w  w. ja v  a 2  s .co m

    response.setContentType("application/json;charset=UTF-8");
    PrintWriter out = response.getWriter();
    if (r.get(0) == "error") {
        JSONObject obj = new JSONObject();
        obj.put("isError", true);
        obj.put("errorDescrip", r.get(1));
        out.print(obj);
    } else if (r.get(0) == "isExitoso") {
        JSONObject obj = new JSONObject();
        obj.put("Exitoso", true);
        out.print(obj);
    } else
        Util.errordeRespuesta(r, out);
}

From source file:co.edu.UNal.ArquitecturaDeSoftware.Bienestar.Vista.App.Usuario.Read.java

protected static void registrarATallerDocenteByDoc(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    ArrayList r = CtrlUsuario.registrarATallerDocenteByDoc(request.getParameter("1"),
            Integer.parseInt(request.getParameter("2"))); // parameter 1: documentoDocente param2: idTaller

    response.setContentType("application/json;charset=UTF-8");
    PrintWriter out = response.getWriter();
    if (r.get(0) == "error") {
        JSONObject obj = new JSONObject();
        obj.put("isError", true);
        obj.put("errorDescrip", r.get(1));
        out.print(obj);//  w  ww. j a v a  2s  .co  m
    } else if (r.get(0) == "isExitoso") {
        JSONObject obj = new JSONObject();
        obj.put("Exitoso", true);
        out.print(obj);
    } else {
        Util.errordeRespuesta(r, out);
    }
}

From source file:co.edu.UNal.ArquitecturaDeSoftware.Bienestar.Vista.App.Usuario.Read.java

protected static void quitarUsuarioConvocatoria(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    ArrayList r = CtrlUsuario.abandonarConvocatoria(Integer.parseInt(request.getParameter("1")), //1. idUsuario
            Integer.parseInt(request.getParameter("2"))// 2. idEvento
    );/*from w w  w.ja  va 2 s . com*/

    response.setContentType("application/json;charset=UTF-8");
    PrintWriter out = response.getWriter();
    if (r.get(0) == "error") {
        JSONObject obj = new JSONObject();
        obj.put("isError", true);
        obj.put("errorDescrip", r.get(1));
        out.print(obj);
    } else if (r.get(0) == "isExitoso") {
        JSONObject obj = new JSONObject();
        obj.put("Exitoso", true);
        out.print(obj);
    } else {
        Util.errordeRespuesta(r, out);
    }
}