com.jayway.jaxrs.hateoas.web.RequestContextFilter.java Source code

Java tutorial

Introduction

Here is the source code for com.jayway.jaxrs.hateoas.web.RequestContextFilter.java

Source

/*
 * Copyright 2011 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jayway.jaxrs.hateoas.web;

import org.apache.commons.lang.StringUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.UriBuilder;
import java.io.IOException;

/**
 * @author Mattias Hellborg Arthursson
 * @author Kalle Stenflo
 */
public class RequestContextFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {

        final HttpServletRequest servletRequest = (HttpServletRequest) request;

        String requestURI = servletRequest.getRequestURI();
        requestURI = StringUtils.removeStart(requestURI,
                servletRequest.getContextPath() + servletRequest.getServletPath());
        String baseURL = StringUtils.removeEnd(servletRequest.getRequestURL().toString(), requestURI);
        UriBuilder uriBuilder = UriBuilder.fromUri(baseURL);

        RequestContext ctx = new RequestContext(uriBuilder,
                servletRequest.getHeader(RequestContext.HATEOAS_OPTIONS_HEADER));

        RequestContext.setRequestContext(ctx);
        try {
            chain.doFilter(request, response);
        } finally {
            RequestContext.clearRequestContext();
        }
    }

    @Override
    public void destroy() {

    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }
}