Example usage for javax.servlet.http HttpServletResponse getHeader

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

Introduction

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

Prototype

public String getHeader(String name);

Source Link

Document

Gets the value of the response header with the given name.

Usage

From source file:cn.sel.wetty.interceptor.AccessLogger.java

private String getHeaders(HttpServletResponse response) {
    List<String> headers = response.getHeaderNames().stream().collect(Collectors.toList());
    for (int i = 0; i < headers.size(); i++) {
        String header = headers.get(i);
        headers.set(i, String.format("%s=%s", header, response.getHeader(header)));
    }/* www  .j  a  va  2s .  co m*/
    return Arrays.toString(headers.toArray(new String[headers.size()]));
}

From source file:net.eusashead.hateoas.conditional.interceptor.ConditionalResponseInterceptor.java

/**
 * After putting a resource//from   w  ww. ja v a  2  s . c o  m
 * update the ETag stored
 * to the new value
 * to ensure that clients
 * are required to re-request
 * the resource and get the latest
 * 
 * NB: Kills all ETags that might
 * have existed under this path for
 * different query strings
 * 
 * @param request
 * @param response
 */
private void postHandlePut(HttpServletRequest request, HttpServletResponse response) {
    service.evictAll(getUri(request));
    if (response.getHeader(ETAG) != null) {
        service.store(getUri(request), response.getHeader(ETAG));
    }

}

From source file:com.lynn.controller.HttpAction.java

@RequestMapping("/header1.do")
@ResponseBody/*ww w. ja va2 s . c o  m*/
public String header1(HttpServletRequest request, HttpServletResponse response) {
    Enumeration e = request.getHeaderNames();
    String query = request.getQueryString();//??get??
    String etag = request.getHeader("etag");
    String if_none_match = request.getHeader("If-None-Match");
    String host = request.getHeader("host");
    String accept = request.getHeader("accept");
    String referer = request.getHeader("referer");
    String host1 = response.getHeader("host");
    String etag1 = response.getHeader("etag");
    String date1 = response.getHeader("date");
    String server1 = response.getHeader("server");
    return query;
}

From source file:io.hops.hopsworks.api.admin.YarnUIProxyServlet.java

protected void copyResponseEntity(HttpMethod method, HttpServletResponse servletResponse) throws IOException {
    InputStream entity = method.getResponseBodyAsStream();
    if (entity != null) {
        OutputStream servletOutputStream = servletResponse.getOutputStream();
        if (servletResponse.getHeader("Content-Type") == null
                || servletResponse.getHeader("Content-Type").contains("html")) {
            String inputLine;/*from   ww w . j  av  a2 s  .  c om*/
            BufferedReader br = new BufferedReader(new InputStreamReader(entity));

            try {
                int contentSize = 0;
                String source = "http://" + method.getURI().getHost() + ":" + method.getURI().getPort();
                while ((inputLine = br.readLine()) != null) {
                    String outputLine = hopify(inputLine, source) + "\n";
                    byte[] output = outputLine.getBytes(Charset.forName("UTF-8"));
                    servletOutputStream.write(output);
                    contentSize += output.length;
                }
                br.close();
                servletResponse.setHeader("Content-Length", Integer.toString(contentSize));
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            org.apache.hadoop.io.IOUtils.copyBytes(entity, servletOutputStream, 4096, doLog);
        }
    }
}

From source file:io.hops.hopsworks.api.admin.HDFSUIProxyServlet.java

protected void copyResponseEntity(HttpMethod method, HttpServletResponse servletResponse) throws IOException {
    InputStream entity = method.getResponseBodyAsStream();
    if (entity != null) {
        OutputStream servletOutputStream = servletResponse.getOutputStream();
        if (servletResponse.getHeader("Content-Type") == null
                || servletResponse.getHeader("Content-Type").contains("html")) {
            String inputLine;/*from w  w  w .  j  av a 2s.  c om*/
            BufferedReader br = new BufferedReader(new InputStreamReader(entity));

            try {
                int contentSize = 0;
                while ((inputLine = br.readLine()) != null) {
                    String outputLine = hopify(inputLine, targetUri) + "\n";
                    byte[] output = outputLine.getBytes(Charset.forName("UTF-8"));
                    servletOutputStream.write(output);
                    contentSize += output.length;
                }
                br.close();
                servletResponse.setHeader("Content-Length", Integer.toString(contentSize));
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            org.apache.hadoop.io.IOUtils.copyBytes(entity, servletOutputStream, 4096, doLog);
        }
    }
}

From source file:com.sangupta.httpd.HttpdHandler.java

/**
 * Log the request details to screen/*from w ww.  j  a  v a  2s .  c  om*/
 * 
 * @param request
 * @param response
 * @param requestTime 
 */
private void logRequest(HttpServletRequest request, HttpServletResponse response, long requestTime) {
    SimpleDateFormat format = new SimpleDateFormat("dd/MMM/yyyy hh:mm:ss");

    StringBuilder builder = new StringBuilder(1024);
    builder.append(request.getRemoteAddr());
    builder.append(" - [");
    builder.append(format.format(new Date(requestTime)));
    builder.append("] \"");
    builder.append(request.getMethod());
    builder.append(' ');
    builder.append(request.getRequestURI());
    builder.append(' ');
    builder.append(request.getProtocol());
    builder.append(" - ");
    builder.append(response.getStatus());
    builder.append(" - ");

    String length = response.getHeader("Content-Length");
    if (length == null) {
        length = "0";
    }

    builder.append(length);

    System.out.println(builder.toString());
}

From source file:org.frontcache.FrontCacheEngine.java

private void addResponseHeaders(WebResponse webResponse, RequestContext context) {
    HttpServletResponse servletResponse = context.getResponse();

    servletResponse.setStatus(webResponse.getStatusCode());

    servletResponse.addHeader(FCHeaders.X_FRONTCACHE_ID, fcHostId);
    servletResponse.addHeader(FCHeaders.X_FRONTCACHE_REQUEST_ID, context.getRequestId());

    if (webResponse.getHeaders() != null) {
        for (String name : webResponse.getHeaders().keySet()) {
            for (String value : webResponse.getHeaders().get(name)) {

                if (null == servletResponse.getHeader(name)) // if header already exist (e.g. in case of WebFilter) - do not duplicate
                    servletResponse.addHeader(name, value);
            }/*from  www  .  ja  v  a  2s  . c o  m*/
        }
    }
}

From source file:org.smigo.log.LogHandler.java

public String getRequestDump(HttpServletRequest request, HttpServletResponse response, String separator) {
    StringBuilder s = new StringBuilder("####REQUEST ").append(request.getMethod()).append(" ")
            .append(request.getRequestURL()).append(separator);
    s.append("Auth type:").append(request.getAuthType()).append(separator);
    s.append("Principal:").append(request.getUserPrincipal()).append(separator);
    s.append(Log.create(request, response).toString()).append(separator);
    s.append("Headers:").append(separator);
    Enumeration<String> headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String headerName = headerNames.nextElement();
        s.append(headerName).append("=").append(request.getHeader(headerName)).append(separator);
    }//w  w  w. j  a  va  2s  .c o  m
    s.append(separator);
    s.append("####RESPONSE").append(separator);
    s.append("Status:").append(response.getStatus()).append(separator);
    s.append("Char encoding:").append(response.getCharacterEncoding()).append(separator);
    s.append("Locale:").append(response.getLocale()).append(separator);
    s.append("Content type:").append(response.getContentType()).append(separator);

    s.append("Headers:").append(separator);
    s.append(response.getHeaderNames().stream().map(rh -> rh + "=" + response.getHeader(rh))
            .collect(Collectors.joining(separator)));

    final Long start = (Long) request.getAttribute(RequestLogFilter.REQUEST_TIMER);
    if (start != null) {
        final long elapsedTime = System.nanoTime() - start;
        s.append(separator).append("####Request time elapsed:").append(elapsedTime);
        s.append("ns which is ").append(elapsedTime / 1000000).append("ms").append(separator);
    }
    return s.toString();
}

From source file:com.groupon.odo.Proxy.java

/**
 * @param httpServletResponse//from   w  w  w .j  a  va 2  s .c o  m
 * @param outStream
 * @param jsonpCallback
 * @throws IOException
 */
private void writeResponseOutput(HttpServletResponse httpServletResponse, OutputStream outStream,
        String jsonpCallback) throws IOException {
    RequestInformation requestInfo = requestInformation.get();

    // check to see if this is chunked
    boolean chunked = false;
    if (httpServletResponse.containsHeader(HttpUtilities.STRING_TRANSFER_ENCODING) && httpServletResponse
            .getHeader(HttpUtilities.STRING_TRANSFER_ENCODING).compareTo("chunked") == 0) {
        httpServletResponse.setHeader(HttpUtilities.STRING_CONNECTION, HttpUtilities.STRING_CHUNKED);
        chunked = true;
    }

    // reattach JSONP if needed
    if (requestInfo.outputString != null && jsonpCallback != null) {
        requestInfo.outputString = jsonpCallback + "(" + requestInfo.outputString + ");";
    }

    // don't do this if we got a HTTP 304 since there is no data to send back
    if (httpServletResponse.getStatus() != HttpServletResponse.SC_NOT_MODIFIED) {
        logger.info("Chunked: {}, {}", chunked, httpServletResponse.getBufferSize());
        if (!chunked) {
            // change the content length header to the new length
            if (requestInfo.outputString != null) {
                httpServletResponse.setContentLength(requestInfo.outputString.getBytes().length);
            } else {
                httpServletResponse.setContentLength(((ByteArrayOutputStream) outStream).toByteArray().length);
            }
        }

        OutputStream outputStreamClientResponse = httpServletResponse.getOutputStream();

        if (requestInfo.outputString != null) {
            outputStreamClientResponse.write(requestInfo.outputString.getBytes());
        } else {
            outputStreamClientResponse.write(((ByteArrayOutputStream) outStream).toByteArray());
        }
        httpServletResponse.flushBuffer();

        logger.info("Done writing");
    }

    // outstr might still be null.. let's try to set it from outStream
    if (requestInfo.outputString == null) {
        try {
            requestInfo.outputString = outStream.toString();
        } catch (Exception e) {
            // can ignore any issues.. worst case outstr is still null
        }
    }
}

From source file:com.mydomain.teavmfirstapp.Server.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, FileNotFoundException, IOException {
    try {/*from ww w.ja  v  a 2s.  c  om*/
        Thread.sleep(500);
    } catch (InterruptedException e) {
        return;
    }
    String action = req.getHeader("action");
    if (action.equals("loadData") == true) {
        try {
            JSONObject json = (JSONObject) parser.parse(
                    new InputStreamReader(getClass().getClassLoader().getResourceAsStream("/com/DBjson.json")));
            json.writeJSONString(resp.getWriter());
        } catch (ParseException e) {
            e.printStackTrace();
        }
    } else if (action.equals("saveData") == true) {
        String JSON = req.getHeader("JSON");
        try {
            File myFile = new File(getClass().getClassLoader().getResource("/com/DBjson.json").getPath());
            FileOutputStream fileStream = new FileOutputStream(myFile, false);
            byte[] myBytes = JSON.getBytes();
            fileStream.write(myBytes);
            fileStream.close();
            System.out.println(
                    "? ?. ,  ? - ");
            try {
                JSONObject json = (JSONObject) parser.parse(new InputStreamReader(
                        getClass().getClassLoader().getResourceAsStream("/com/DBjson.json")));
                System.out.println(json.toJSONString());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            resp.getHeader("NAME");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}