Example usage for javax.servlet.http HttpServletResponse reset

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

Introduction

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

Prototype

public void reset();

Source Link

Document

Clears any data that exists in the buffer as well as the status code, headers.

Usage

From source file:com.aurel.track.dbase.ReadyTesterServlet.java

private void prepareResponse(HttpServletResponse httpServletResponse) {
    try {/*from w  w w. j  a v a  2s  .  c om*/
        httpServletResponse.reset();
        httpServletResponse.setCharacterEncoding("UTF-8");
    } catch (Exception e) {
        //when the <s:action/> is present on the jsp
        //(mainly getting root nodes the same response object is used, and second time it can't be reseted)
    }
    httpServletResponse.setContentType("application/json");
}

From source file:com.intel.rsa.podm.rest.filters.UrlValidationFilter.java

private void setErrorResponse(HttpServletResponse httpServletResponse, ErrorResponse error) {
    httpServletResponse.reset();
    httpServletResponse.resetBuffer();//from w  ww.j  a va2s .c o  m

    //we have to add since this request is outside our app
    httpServletResponse.addHeader("OData-Version", "4.0");
    httpServletResponse.addHeader("Cache-Control", "max-age=0");
    httpServletResponse.addHeader("Access-Control-Allow-Origin", "*");

    httpServletResponse.setCharacterEncoding(DEFAULT_ENCODING_HTTP);
    httpServletResponse.setContentType(MediaType.APPLICATION_JSON);
    httpServletResponse.setStatus(error.getErrorCode());

    try (PrintWriter responseWriter = httpServletResponse.getWriter()) {
        responseWriter.print(getJsonResponse(error));
        responseWriter.flush();
    } catch (IOException e) {
        logger.e("Unable to write response. Sending empty body response.", e);
    }
}

From source file:com.usefullc.platform.common.utils.IOUtils.java

/**
 * /* ww  w  .j a  v a 2  s. c  om*/
 * 
 * @param path
 * @param fileName
 * @param response
 * @return
 */
public static void download(String path, String fileName, HttpServletResponse response) {
    try {

        if (StringUtils.isEmpty(path)) {
            throw new IllegalArgumentException("?");
        } else {
            File file = new File(path);
            if (!file.exists()) {
                throw new IllegalArgumentException("??");
            }
        }
        if (StringUtils.isEmpty(fileName)) {
            throw new IllegalArgumentException("???");
        }
        if (response == null) {
            throw new IllegalArgumentException("response ?");
        }

        // path
        File file = new File(path);

        // ??
        InputStream fis = new BufferedInputStream(new FileInputStream(path));
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();
        // response
        response.reset();

        // ??linux ?  linux utf-8,windows  GBK)
        String defaultEncoding = System.getProperty("file.encoding");
        if (defaultEncoding != null && defaultEncoding.equals("UTF-8")) {

            response.addHeader("Content-Disposition",
                    "attachment;filename=" + new String(fileName.getBytes("GBK"), "iso-8859-1"));
        } else {
            response.addHeader("Content-Disposition",
                    "attachment;filename=" + new String(fileName.getBytes(), "iso-8859-1"));
        }

        // responseHeader
        response.addHeader("Content-Length", "" + file.length());
        response.setContentType("application/octet-stream");
        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
        toClient.write(buffer);
        toClient.flush();
        toClient.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:fll.web.api.ChallengeDescriptionServlet.java

@Override
protected final void doGet(final HttpServletRequest request, final HttpServletResponse response)
        throws IOException, ServletException {
    final ServletContext application = getServletContext();

    response.reset();
    response.setContentType("application/json");
    final PrintWriter writer = response.getWriter();

    final ObjectMapper jsonMapper = new ObjectMapper();

    final ChallengeDescription challengeDescription = ApplicationAttributes
            .getChallengeDescription(application);

    jsonMapper.writeValue(writer, challengeDescription);
}

From source file:fll.web.api.challengeDescription.SubjectiveCategories.java

@Override
protected final void doGet(final HttpServletRequest request, final HttpServletResponse response)
        throws IOException, ServletException {
    final ServletContext application = getServletContext();

    response.reset();
    response.setContentType("application/json");
    final PrintWriter writer = response.getWriter();

    final ObjectMapper jsonMapper = new ObjectMapper();

    final ChallengeDescription challengeDescription = ApplicationAttributes
            .getChallengeDescription(application);
    final List<ScoreCategory> categories = challengeDescription.getSubjectiveCategories();

    jsonMapper.writeValue(writer, categories);
}

From source file:de.u808.simpleinquest.web.download.DownloadController.java

private void handleDownload(HttpServletResponse response, File file) throws IOException {
    response.reset();
    response.setContentType(mimeTypeRegistry.getMimeType(file));
    response.setHeader("Pragma", "private");
    response.setHeader("Cache-Control", "private, must-revalidate");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");

    FileInputStream fis = new FileInputStream(file);
    OutputStream os = response.getOutputStream();
    int len = -1;
    int x = 0;/*from   w w w. ja va  2  s.  c  om*/
    while ((len = fis.read(buf)) != -1) {
        os.write(buf, 0, len);
    }

    response.getOutputStream().flush();
    response.getOutputStream().close();
}

From source file:com.lbb.controller.FileUploadController.java

@RequestMapping("download.html")
public void download(String fileName, HttpServletResponse response) throws IOException {
    OutputStream os = response.getOutputStream();
    try {/*from  w w  w.ja  v  a 2s.  c o  m*/
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        response.setContentType("image/jpeg; charset=utf-8");
        os.write(FileUtils.readFileToByteArray(FileUpload.getFile(fileName)));
        os.flush();
    } finally {
        if (os != null) {
            os.close();
        }
    }
}

From source file:com.pearson.developer.xapi.proxy.AuthFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws java.io.IOException, ServletException {

    HttpServletRequest req = (HttpServletRequest) request;

    // AJAX will preflight xAPI request with OPTIONS request without Authorization header
    if ("OPTIONS".equals(req.getMethod())) {
        chain.doFilter(request, response);
        return;//  w w  w. jav a  2 s  . c o m
    }

    boolean authorized = false;
    try { // decode and verify the basic auth credentials
        String authHeader = req.getHeader("Authorization");
        authHeader = authHeader.substring("Basic ".length());
        String decodedAuthHeader = new String(Base64.decodeBase64(authHeader), "UTF-8");
        String[] credentials = decodedAuthHeader.split(":");
        if (credentials.length == 2) {
            String username = credentials[0];
            String password = credentials[1];
            authorized = SessionDatabase.verify(username, password);
        }
    } catch (Exception e) {
        // do nothing
    }

    // proceed to xAPI if session was authorized
    if (authorized) {
        final String targetBasicAuth = config.getInitParameter("targetBasicAuth");

        // need to give the LRS it's expected Authorization value
        HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper(req) {
            @Override
            public String getHeader(String name) {
                if ("Authorization".equalsIgnoreCase(name)) {
                    return targetBasicAuth;
                }
                return super.getHeader(name);
            }

            @Override
            public Enumeration<String> getHeaders(String name) {
                if ("Authorization".equalsIgnoreCase(name)) {
                    List<String> values = new ArrayList<String>();
                    values.add(targetBasicAuth);
                    return Collections.enumeration(values);
                }
                return super.getHeaders(name);
            }
        };
        chain.doFilter(requestWrapper, response);
        return;
    }

    // respond with a 401 if missing auth
    HttpServletResponse resp = (HttpServletResponse) response;
    resp.reset();
    resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    resp.getWriter().println("401 - Unauthorized");
}

From source file:com.zuoxiaolong.blog.web.controller.AbstractWebController.java

/**
 * //from   ww w  .j  a v a2  s. co m
 *
 * @param string
 * @return
 */
protected void renderText(String string, String type) {
    try {
        HttpServletResponse response = getResponse();
        response.reset();
        response.setContentType(type);
        response.setCharacterEncoding("utf-8");
        response.getWriter().print(string);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.palantir.stash.stashbot.servlet.BuildStatusReportingServlet.java

private void printOutput(JSONObject output, HttpServletRequest req, HttpServletResponse res)
        throws IOException {
    res.reset();
    res.setStatus(200);/*from  w w w.  j  av a 2 s. co  m*/
    res.setContentType("application/json;charset=UTF-8");
    Writer w = res.getWriter();
    try {
        w.append(output.toString(4));
    } catch (JSONException e) {
        w.append(output.toString());
    }
    w.close();
}