Skip to content

Commit

Permalink
Merge "Updated to Guice 3.0."
Browse files Browse the repository at this point in the history
  • Loading branch information
spearce authored and gerrit code review committed Mar 7, 2012
2 parents 90e96be + 7820803 commit 2a437a7
Show file tree
Hide file tree
Showing 23 changed files with 171 additions and 209 deletions.
1 change: 0 additions & 1 deletion gerrit-gwtui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ limitations under the License.
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-user</artifactId>
<version>${gwtVersion}</version>
</dependency>

<dependency>
Expand Down
2 changes: 2 additions & 0 deletions gerrit-pgm/src/main/java/com/google/gerrit/pgm/Daemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.gerrit.httpd.WebSshGlueModule;
import com.google.gerrit.httpd.auth.openid.OpenIdModule;
import com.google.gerrit.lifecycle.LifecycleManager;
import com.google.gerrit.pgm.http.jetty.GetUserFilter;
import com.google.gerrit.pgm.http.jetty.JettyEnv;
import com.google.gerrit.pgm.http.jetty.JettyModule;
import com.google.gerrit.pgm.http.jetty.ProjectQoSFilter;
Expand Down Expand Up @@ -274,6 +275,7 @@ private Injector createWebInjector() {
if (authConfig.getAuthType() == AuthType.OPENID) {
modules.add(new OpenIdModule());
}
modules.add(sysInjector.getInstance(GetUserFilter.Module.class));

return sysInjector.createChildInjector(modules);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (C) 2012 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.google.gerrit.pgm.http.jetty;

import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.google.inject.servlet.ServletModule;

import org.eclipse.jgit.lib.Config;

import java.io.IOException;
import java.net.URI;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
* Stores as a request attribute, so the {@link HttpLog} can include the the
* user for the request outside of the request scope.
*
* @author [email protected] (Colby Ranger)
*/
@Singleton
public class GetUserFilter implements Filter {

static final String REQ_ATTR_KEY = CurrentUser.class.toString();

public static class Module extends ServletModule {

private boolean loggingEnabled;

@Inject
Module(@GerritServerConfig final Config cfg) {
URI[] urls = JettyServer.listenURLs(cfg);
boolean reverseProxy = JettyServer.isReverseProxied(urls);
this.loggingEnabled = cfg.getBoolean("httpd", "requestlog", !reverseProxy);
}

@Override
protected void configureServlets() {
if (loggingEnabled) {
filter("/*").through(GetUserFilter.class);
}
}
}

private final Provider<CurrentUser> userProvider;

@Inject
GetUserFilter(final Provider<CurrentUser> userProvider) {
this.userProvider = userProvider;
}

@Override
public void doFilter(
ServletRequest req, ServletResponse resp, FilterChain chain)
throws IOException, ServletException {
req.setAttribute(REQ_ATTR_KEY, userProvider.get());
chain.doFilter(req, resp);
}

@Override
public void destroy() {
}

@Override
public void init(FilterConfig arg0) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.config.SitePaths;
import com.google.inject.Provider;
import com.google.inject.servlet.GuiceHelper;

import org.apache.log4j.Appender;
import org.apache.log4j.AsyncAppender;
Expand Down Expand Up @@ -55,11 +53,8 @@ class HttpLog extends AbstractLifeCycle implements RequestLog {
private static final String P_USER_AGENT = "User-Agent";

private final AsyncAppender async;
private final Provider<CurrentUser> userProvider;

HttpLog(final SitePaths site, final Provider<CurrentUser> userProvider) {
this.userProvider = userProvider;

HttpLog(final SitePaths site) {
final DailyRollingFileAppender dst = new DailyRollingFileAppender();
dst.setName(LOG_NAME);
dst.setLayout(new MyLayout());
Expand Down Expand Up @@ -91,15 +86,11 @@ protected void doStop() throws Exception {

@Override
public void log(final Request req, final Response rsp) {
GuiceHelper.runInContext(req, rsp, new Runnable() {
@Override
public void run() {
doLog(req, rsp);
}
});
CurrentUser user = (CurrentUser) req.getAttribute(GetUserFilter.REQ_ATTR_KEY);
doLog(req, rsp, user);
}

private void doLog(Request req, Response rsp) {
private void doLog(Request req, Response rsp, CurrentUser user) {
final LoggingEvent event = new LoggingEvent( //
Logger.class.getName(), // fqnOfCategoryClass
null, // logger (optional)
Expand All @@ -119,7 +110,6 @@ private void doLog(Request req, Response rsp) {
uri = uri + "?" + qs;
}

CurrentUser user = userProvider.get();
if (user instanceof IdentifiedUser) {
IdentifiedUser who = (IdentifiedUser) user;
if (who.getUserName() != null && !who.getUserName().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@
import com.google.gerrit.launcher.GerritLauncher;
import com.google.gerrit.lifecycle.LifecycleListener;
import com.google.gerrit.reviewdb.AuthType;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.config.ConfigUtil;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.config.SitePaths;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.google.inject.servlet.GuiceFilter;
import com.google.inject.servlet.GuiceHelper;
import com.google.inject.servlet.GuiceServletContextListener;

import org.eclipse.jetty.io.EndPoint;
Expand Down Expand Up @@ -68,10 +65,6 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Singleton
public class JettyServer {
static class Lifecycle implements LifecycleListener {
Expand Down Expand Up @@ -112,7 +105,7 @@ public void stop() {

@Inject
JettyServer(@GerritServerConfig final Config cfg, final SitePaths site,
final JettyEnv env, final Provider<CurrentUser> userProvider)
final JettyEnv env)
throws MalformedURLException, IOException {
this.site = site;

Expand All @@ -122,24 +115,8 @@ public void stop() {

Handler app = makeContext(env, cfg);
if (cfg.getBoolean("httpd", "requestlog", !reverseProxy)) {
RequestLogHandler handler = new RequestLogHandler() {
@Override
public void handle(String target, Request baseRequest,
final HttpServletRequest req, final HttpServletResponse rsp)
throws IOException, ServletException {
// Force the user to construct, so it's available to our HttpLog
// later on when the request gets logged out to the access file.
//
GuiceHelper.runInContext(req, rsp, new Runnable() {
@Override
public void run() {
userProvider.get();
}
});
super.handle(target, baseRequest, req, rsp);
}
};
handler.setRequestLog(new HttpLog(site, userProvider));
RequestLogHandler handler = new RequestLogHandler();
handler.setRequestLog(new HttpLog(site));
handler.setHandler(app);
app = handler;
}
Expand All @@ -164,7 +141,7 @@ private Connector[] listen(final Config cfg) {
final int acceptors = cfg.getInt("httpd", "acceptorThreads", 2);
final AuthType authType = ConfigUtil.getEnum(cfg, "auth", null, "type", AuthType.OPENID);

reverseProxy = true;
reverseProxy = isReverseProxied(listenUrls);
final Connector[] connectors = new Connector[listenUrls.length];
for (int idx = 0; idx < listenUrls.length; idx++) {
final URI u = listenUrls[idx];
Expand All @@ -179,7 +156,6 @@ private Connector[] listen(final Config cfg) {
}

if ("http".equals(u.getScheme())) {
reverseProxy = false;
defaultPort = 80;
c = new SelectChannelConnector();
} else if ("https".equals(u.getScheme())) {
Expand All @@ -198,7 +174,6 @@ private Connector[] listen(final Config cfg) {
ssl.setNeedClientAuth(true);
}

reverseProxy = false;
defaultPort = 443;
c = ssl;

Expand Down Expand Up @@ -256,7 +231,16 @@ public void customize(EndPoint endpoint, Request request)
return connectors;
}

private URI[] listenURLs(final Config cfg) {
static boolean isReverseProxied(final URI[] listenUrls) {
for (URI u : listenUrls) {
if ("http".equals(u.getScheme()) || "https".equals(u.getScheme())) {
return false;
}
}
return true;
}

static URI[] listenURLs(final Config cfg) {
String[] urls = cfg.getStringList("httpd", null, "listenurl");
if (urls.length == 0) {
urls = new String[] {"http://*:8080/"};
Expand Down

This file was deleted.

6 changes: 3 additions & 3 deletions gerrit-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,17 @@ limitations under the License.
</dependency>

<dependency>
<groupId>com.google.code.guice</groupId>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</dependency>

<dependency>
<groupId>com.google.code.guice</groupId>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-servlet</artifactId>
</dependency>

<dependency>
<groupId>com.google.code.guice</groupId>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-assistedinject</artifactId>
</dependency>

Expand Down
Loading

0 comments on commit 2a437a7

Please sign in to comment.