Example usage for javax.servlet.http HttpServletResponse addHeader

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

Introduction

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

Prototype

public void addHeader(String name, String value);

Source Link

Document

Adds a response header with the given name and value.

Usage

From source file:fr.lille1_univ.car_tprest.jetty.web.UpweeController.java

/**
* A call that is used to download the file in the pat corresponding to the filename
*//*w ww.j ava2s. co m*/
@CrossOrigin
@RequestMapping(method = {
        RequestMethod.GET }, value = "/api/files/download/**", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public void download(HttpServletRequest request, @RequestParam(value = "file", required = true) String filename,
        HttpServletResponse response) {
    try {
        response.addHeader("Content-disposition", "attachment;filename=" + filename);
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        String decodedfileName = "";
        decodedfileName = java.net.URLDecoder.decode(filename, "UTF-8");
        InputStream stream = this.fileSystemService
                .getFile(getFullFileRequestPath("/api/files/download/**", request, decodedfileName));
        IOUtils.copy(stream, response.getOutputStream());
        response.flushBuffer();
        stream.close();

    } catch (Exception e) {
        l.e("Unable to get file");
        e.printStackTrace();
    }
}

From source file:pl.maciejwalkowiak.plist.spring.PlistView.java

@Override
protected void prepareResponse(HttpServletRequest request, HttpServletResponse response) {
    response.setContentType(getContentType());
    response.setCharacterEncoding(ENCODING);
    if (this.disableCaching) {
        response.addHeader("Pragma", "no-cache");
        response.addHeader("Cache-Control", "no-cache, no-store, max-age=0");
        response.addDateHeader("Expires", 1L);
    }//from  w w w  .j  a v a  2  s  .c  om
}

From source file:GetMachine.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from   w ww  .  j  a  va 2  s .co m
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("application/json");
    response.addHeader("Access-Control-Allow-Origin", "*");
    try {
        /* TODO output your page here. You may use following sample code. */
        PrintWriter out = response.getWriter();
        connect = getConnection();
        statement = connect.createStatement();
        resultSet = statement.executeQuery("SELECT distinct(machine) FROM lab3.logs");
        JSONArray ja = new JSONArray();
        while (resultSet.next()) {
            JSONObject obj = new JSONObject();
            obj.put("id", resultSet.getString(1));
            obj.put("reg", resultSet.getString(1));
            ja.put(obj);
        }
        out.println(ja);
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:com.dreamwork.web.FrontJsonController.java

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String requestID = request.getHeader("RequestID");
    response.addHeader("RequestID", requestID);
    response.setContentType("text/json; charset=UTF-8");
    ServletInputStream in = request.getInputStream();
    ServletOutputStream out = response.getOutputStream();
    String req = "";
    try {/*from   w  w w.  java  2 s . co  m*/
        req = new String(read(in), "UTF-8");

        JSONObject jsonObject = new JSONObject(req);
        JSONObject requestJson = jsonObject.getJSONObject("request");
        String to = requestJson.getString("to");
        String date = requestJson.getString("date");
        String message = requestJson.getString("message");
        ServiceHolder.getService().receiveMessage(to, df.parse(date), message);

    } catch (JSONException e) {

    } catch (IOException e) {

    } catch (RuntimeException e) {

    } catch (Exception e) {

    } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }
}

From source file:org.openwms.core.uaa.api.RolesController.java

/**
 * Documented here: https://openwms.atlassian.net/wiki/x/BIAWAQ
 *
 * @param role The {@link Role} instance to be created
 * @return An {@link Response} object to encapsulate the result of the creation operation
 * @status Reviewed [scherrer]//www .  j a va 2  s.co m
 */
@PostMapping
@ResponseBody
public ResponseEntity<Response<RoleVO>> create(@RequestBody @Valid @NotNull RoleVO role, HttpServletRequest req,
        HttpServletResponse resp) {
    RoleVO createdRole = m.map(service.save(m.map(role, Role.class)), RoleVO.class);
    resp.addHeader(HttpHeaders.LOCATION, getLocationForCreatedResource(req, createdRole.getId().toString()));
    return buildResponse(HttpStatus.CREATED, translate(Messages.CREATED), Messages.CREATED);
}

From source file:com.auction.servlet.FileUploadServlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//w  w  w.  j a  va2  s  .c  om
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType("text/html;charset=UTF-8");
    response.addHeader("Access-Control-Allow-Origin", "*");
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */

        boolean isMultipart = ServletFileUpload.isMultipartContent(request);

        if (isMultipart) {
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);

            try {
                String randomFileName = StringUtils.getRandomString() + ".jpg";
                List items = upload.parseRequest(request);
                Iterator iterator = items.iterator();
                while (iterator.hasNext()) {
                    FileItem item = (FileItem) iterator.next();

                    if (!item.isFormField()) {
                        //String fileName = item.getName();

                        String root = getServletContext().getRealPath("/");
                        File path = new File(root + "/uploads");
                        if (!path.exists()) {
                            boolean status = path.mkdirs();
                        }

                        //File uploadedFile = new File(path + "/" + fileName);
                        File uploadedFile = new File(path + "/" + randomFileName);
                        System.out.println(uploadedFile.getAbsolutePath());
                        item.write(uploadedFile);
                    }
                }
                out.print(randomFileName);
            } catch (FileUploadException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:io.dacopancm.jfee.managedController.ReportBean.java

public void sendPdf(JasperPrint jasperPrint, String filename) throws JRException, IOException {
    HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance()
            .getExternalContext().getResponse();
    httpServletResponse.addHeader("Content-disposition", "attachment; filename=" + filename + ".pdf");
    ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
    JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);
    FacesContext.getCurrentInstance().responseComplete();

}

From source file:net.sourceforge.fenixedu.presentationTier.Action.externalServices.epfl.ExportPhdIndividualProgramProcessInformation.java

private void exportInformationXml(final HttpServletRequest request, final HttpServletResponse response)
        throws Exception {
    response.addHeader("Content-Disposition", "attachment; filename=epfl.xml");
    response.setContentType(Charset.defaultCharset().name());
    final byte[] content = ExportEPFLPhdProgramCandidacies.run();
    writeResponse(response, content, "application/xml");
}

From source file:CacheFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    for (int n = 0; n < replyHeaders.length; n++) {
        String name = replyHeaders[n][0];
        String value = replyHeaders[n][1];
        httpResponse.addHeader(name, value);
    }/*from  ww w .j  av a  2  s.c  o m*/
    chain.doFilter(request, response);
}

From source file:org.kemri.wellcome.controller.ReportDefinitionController.java

@RequestMapping(value = Views.EXPORT, method = RequestMethod.GET)
public void export(HttpServletResponse response) throws Exception {
    response.setContentType("application/xml");
    response.setCharacterEncoding("UTF-8");
    response.addHeader("Content-Disposition", "attachment; filename=ReportDefinition.xml");
    ReportTemplates templates = service.getReportTemplates();
    if (templates != null)
        service.marshallReportTemplates(response.getOutputStream(), templates);
}