// Path: github-oauth/src/main/java/com/google/gerrit/httpd/XGerritAuth.java
// @Singleton
// public class XGerritAuth {
//   public static final String X_GERRIT_AUTH = "X-Gerrit-Auth";
//   private WebSessionManager manager;
// 
//   @Inject
//   public XGerritAuth(
//       WebSessionManagerFactory managerFactory,
//       @Named(WebSessionManager.CACHE_NAME) Cache<String, Val> cache) {
//     this.manager = managerFactory.create(cache);
//   }
// 
//   public String getAuthValue(Cookie gerritCookie) {
//     Val session = manager.get(new WebSessionManager.Key(gerritCookie.getValue()));
//     return session.getAuth();
//   }
// }


import org.slf4j.LoggerFactory;
import com.google.common.collect.Sets;
import com.google.gerrit.httpd.GitOverHttpServlet;
import com.google.gerrit.httpd.XGerritAuth;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import java.io.IOException;
import java.util.Set;
import java.util.regex.Pattern;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
// Copyright (C) 2013 The Android Open Source Project
//
// 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.googlesource.gerrit.plugins.github.oauth;


@Singleton
public class OAuthFilter implements Filter {
  private static final org.slf4j.Logger log = LoggerFactory.getLogger(OAuthFilter.class);
  private static Pattern GIT_HTTP_REQUEST_PATTERN = Pattern.compile(GitOverHttpServlet.URL_REGEX);
  private static final Set<String> GERRIT_STATIC_RESOURCES_EXTS =
      Sets.newHashSet("css", "png", "jpg", "gif", "woff", "otf", "ttf", "map", "js", "swf", "txt");
  private static final Set<String> GERRIT_ALLOWED_PATHS = Sets.newHashSet("Documentation");
  private static final Set<String> GERRIT_ALLOWED_PAGES = Sets.newHashSet("scope.html");

  private final GitHubOAuthConfig config;
  private final OAuthWebFilter webFilter;

  @Inject
  public OAuthFilter(GitHubOAuthConfig config, OAuthWebFilter webFilter, Injector injector) {
    this.config = config;
    this.webFilter = webFilter;
  }

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
    webFilter.init(filterConfig);
  }

  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws IOException, ServletException {
    HttpServletRequest httpRequest = (HttpServletRequest) request;

    String requestUrl = httpRequest.getRequestURI();

    if (!config.enabled || skipOAuth(httpRequest)) {
      chain.doFilter(request, response);
// Path: github-oauth/src/main/java/com/google/gerrit/httpd/XGerritAuth.java
// @Singleton
// public class XGerritAuth {
//   public static final String X_GERRIT_AUTH = "X-Gerrit-Auth";
//   private WebSessionManager manager;
// 
//   @Inject
//   public XGerritAuth(
//       WebSessionManagerFactory managerFactory,
//       @Named(WebSessionManager.CACHE_NAME) Cache<String, Val> cache) {
//     this.manager = managerFactory.create(cache);
//   }
// 
//   public String getAuthValue(Cookie gerritCookie) {
//     Val session = manager.get(new WebSessionManager.Key(gerritCookie.getValue()));
//     return session.getAuth();
//   }
// }


// Path: github-oauth/src/main/java/com/googlesource/gerrit/plugins/github/oauth/OAuthFilter.java
import org.slf4j.LoggerFactory;
import com.google.common.collect.Sets;
import com.google.gerrit.httpd.GitOverHttpServlet;
import com.google.gerrit.httpd.XGerritAuth;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import java.io.IOException;
import java.util.Set;
import java.util.regex.Pattern;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;

// Copyright (C) 2013 The Android Open Source Project
//
// 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.googlesource.gerrit.plugins.github.oauth;


@Singleton
public class OAuthFilter implements Filter {
  private static final org.slf4j.Logger log = LoggerFactory.getLogger(OAuthFilter.class);
  private static Pattern GIT_HTTP_REQUEST_PATTERN = Pattern.compile(GitOverHttpServlet.URL_REGEX);
  private static final Set<String> GERRIT_STATIC_RESOURCES_EXTS =
      Sets.newHashSet("css", "png", "jpg", "gif", "woff", "otf", "ttf", "map", "js", "swf", "txt");
  private static final Set<String> GERRIT_ALLOWED_PATHS = Sets.newHashSet("Documentation");
  private static final Set<String> GERRIT_ALLOWED_PAGES = Sets.newHashSet("scope.html");

  private final GitHubOAuthConfig config;
  private final OAuthWebFilter webFilter;

  @Inject
  public OAuthFilter(GitHubOAuthConfig config, OAuthWebFilter webFilter, Injector injector) {
    this.config = config;
    this.webFilter = webFilter;
  }

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
    webFilter.init(filterConfig);
  }

  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws IOException, ServletException {
    HttpServletRequest httpRequest = (HttpServletRequest) request;

    String requestUrl = httpRequest.getRequestURI();

    if (!config.enabled || skipOAuth(httpRequest)) {
      chain.doFilter(request, response);
    } else {