Java Code Examples for javax.servlet.http.HttpServletResponse#addIntHeader()

The following examples show how to use javax.servlet.http.HttpServletResponse#addIntHeader() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: zhcet-web   File: RateLimitInterceptor.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    String key = getKey(request);

    int attempts = rateLimitService.incrementAttempts(key);
    if (isLimitExceeded(request, attempts)) {
        issueWarning(Utils.getClientIP(request), request.getRequestURL());
        response.setStatus(429);
        response.addHeader("Content-Type", "application/json");
        response.getOutputStream().print("{\"message\":\"Rate limit exceeded. Try again later\"}");
        return false;
    }

    response.addIntHeader("Remaining-Requests", getRemainingLimit(request, attempts));
    return true;
}
 
Example 2
protected void copyResponseHeaders(HttpServletResponseImpl source, HttpServletResponse target) {
	Map<String, Object> headerMap = source.getHeaderMap();
	for (Iterator<Map.Entry<String, Object>> itr = headerMap.entrySet().iterator(); itr.hasNext();) {
		Map.Entry<String, Object> entry = itr.next();
		String headerKey = entry.getKey();
		Object headerVal = entry.getValue();

		if (StringUtils.equalsIgnoreCase(CONTENT_TYPE, headerKey)) {
			continue;
		} else if (StringUtils.equalsIgnoreCase(CONTENT_LEN, headerKey)) {
			continue;
		}

		if (Date.class.isInstance(headerVal)) {
			target.addDateHeader(headerKey, ((Date) headerVal).getTime());
		} else if (Integer.class.isInstance(headerVal) || Integer.TYPE.isInstance(headerVal)) {
			target.addIntHeader(headerKey, ((Integer) headerVal).intValue());
		} else {
			target.addHeader(headerKey, headerVal == null ? "" : String.valueOf(headerVal));
		}
	}
}
 
Example 3
Source Project: glowroot   File: ResponseHeaderIT.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
    response.setHeader("One", "abc");
    response.setHeader("one", "ab");
    response.addHeader("one", "xy");
    response.setHeader("Two", "1");
    response.setHeader("Three", "xyz");

    response.setDateHeader("Date-One", 1393553211832L);
    response.setDateHeader("Date-one", 1393553212832L);
    response.addDateHeader("Date-one", 1393553213832L);
    response.setDateHeader("Date-Two", 1393553214832L);
    response.setDateHeader("Date-Thr", 1393553215832L);
    response.addDateHeader("Date-Four", 1393553215832L);

    response.setIntHeader("Int-One", 1);
    response.setIntHeader("Int-one", 2);
    response.addIntHeader("Int-one", 3);
    response.setIntHeader("Int-Two", 4);
    response.setIntHeader("Int-Thr", 5);
    response.addIntHeader("Int-Four", 6);

    response.addHeader("X-One", "xy");
    response.addDateHeader("X-one", 1393553216832L);
    response.addIntHeader("X-one", 6);
}
 
Example 4
protected void insertCacheControlHeader(final HttpServletResponse httpServletResponse, final HttpServletRequest httpServletRequest,
                                        final String cacheControlHeader) {

    final String uri = httpServletRequest.getRequestURI();
    if (!uri.endsWith(".css")
        && !uri.endsWith(".js")
        && !uri.endsWith(".png")
        && !uri.endsWith(".txt")
        && !uri.endsWith(".jpg")
        && !uri.endsWith(".ico")
        && !uri.endsWith(".jpeg")
        && !uri.endsWith(".bmp")
        && !uri.endsWith(".gif")) {
        httpServletResponse.addHeader("Cache-Control", cacheControlHeader);
        httpServletResponse.addHeader("Pragma", "no-cache");
        httpServletResponse.addIntHeader("Expires", 0);
        LOGGER.fine("Adding Cache Control response headers for " + uri);
    }
}
 
Example 5
Source Project: cloudstack   File: HttpUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static void addSecurityHeaders(final HttpServletResponse resp) {
    if (resp.containsHeader("X-Content-Type-Options")) {
        resp.setHeader("X-Content-Type-Options", "nosniff");
    }
    else {
        resp.addHeader("X-Content-Type-Options", "nosniff");
    }
    if (resp.containsHeader("X-XSS-Protection")) {
        resp.setHeader("X-XSS-Protection", "1;mode=block");
    }
    else {
        resp.addHeader("X-XSS-Protection", "1;mode=block");
    }

    if (resp.containsHeader("content-security-policy")) {
        resp.setIntHeader("content-security-policy", 1);
    }else {
        resp.addIntHeader("content-security-policy", 1);
    }
    resp.addHeader("content-security-policy","default-src=none");
    resp.addHeader("content-security-policy","script-src=self");
    resp.addHeader("content-security-policy","connect-src=self");
    resp.addHeader("content-security-policy","img-src=self");
    resp.addHeader("content-security-policy","style-src=self");
}
 
Example 6
Source Project: ymate-platform-v2   File: AbstractView.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public IView addIntHeader(String name, int value) {
    HttpServletResponse _response = WebContext.getResponse();
    if (_response.containsHeader(name)) {
        _response.addIntHeader(name, value);
    } else {
        _response.setIntHeader(name, value);
    }
    return this;
}
 
Example 7
Source Project: maven-framework-project   File: RateLimiter.java    License: MIT License 5 votes vote down vote up
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                     FilterChain filterChain) throws IOException, ServletException {

    HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
    HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;

    String ipAddress = getIpAddress(httpServletRequest);
    if (accessCounter.contains(ipAddress)) {
        if (!requestLimitExceeded(ipAddress)) {
            accessCounter.increment(ipAddress);
            accessCounter.getAccessDetails(ipAddress).setLastUpdated(System.currentTimeMillis());

        } else {

            httpServletResponse.addIntHeader("Retry-After",TIME_LIMIT);
            httpServletResponse.sendError(429);

        }
    } else {
        accessCounter.add(ipAddress);

    }
    filterChain.doFilter(servletRequest, servletResponse);


}
 
Example 8
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    res.addIntHeader("ae", 1 / 0);
}
 
Example 9
/**
 * {@inheritDoc}
 */
public void addToResponse(final HttpServletResponse httpServletResponse) {

   httpServletResponse.addIntHeader(name, value);
}
 
Example 10
Source Project: commons-jcs   File: JCacheFilter.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException
{
    boolean gzip = false;
    if (HttpServletRequest.class.isInstance(servletRequest))
    {
        final Enumeration<String> acceptEncoding = HttpServletRequest.class.cast(servletRequest).getHeaders("Accept-Encoding");
        while (acceptEncoding != null && acceptEncoding.hasMoreElements())
        {
            if ("gzip".equals(acceptEncoding.nextElement()))
            {
                gzip = true;
                break;
            }
        }
    }

    final HttpServletResponse httpServletResponse = HttpServletResponse.class.cast(servletResponse);
    checkResponse(httpServletResponse);

    final PageKey key = new PageKey(key(servletRequest), gzip);
    Page page = cache.get(key);
    if (page == null)
    {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final InMemoryResponse response;
        if (gzip)
        {
            response = new InMemoryResponse(httpServletResponse, new GZIPOutputStream(baos));
        }
        else
        {
            response = new InMemoryResponse(httpServletResponse, baos);
        }
        filterChain.doFilter(servletRequest, response);
        response.flushBuffer();

        page = new Page(
                response.getStatus(),
                response.getContentType(),
                response.getContentLength(),
                response.getCookies(),
                response.getHeaders(),
                baos.toByteArray());
        cache.put(key, page);
    }

    if (page.status == SC_OK) {
        checkResponse(httpServletResponse);

        if (gzip)
        {
            httpServletResponse.setHeader("Content-Encoding", "gzip");
        }

        httpServletResponse.setStatus(page.status);
        if (page.contentType != null)
        {
            httpServletResponse.setContentType(page.contentType);
        }
        if (page.contentLength > 0)
        {
            httpServletResponse.setContentLength(page.contentLength);
        }
        for (final Cookie c : page.cookies)
        {
            httpServletResponse.addCookie(c);
        }
        for (final Map.Entry<String, List<Serializable>> entry : page.headers.entrySet())
        {
            for (final Serializable value : entry.getValue())
            {
                if (Integer.class.isInstance(value))
                {
                    httpServletResponse.addIntHeader(entry.getKey(), Integer.class.cast(value));
                }
                else if (String.class.isInstance(value))
                {
                    httpServletResponse.addHeader(entry.getKey(), String.class.cast(value));
                }
                else if (Long.class.isInstance(value))
                {
                    httpServletResponse.addDateHeader(entry.getKey(), Long.class.cast(value));
                }
            }
        }
        httpServletResponse.setContentLength(page.out.length);
        final BufferedOutputStream bos = new BufferedOutputStream(httpServletResponse.getOutputStream());
        if (page.out.length != 0)
        {
            bos.write(page.out);
        }
        else
        {
            bos.write(new byte[0]);
        }
        bos.flush();
    }
}