Format all Java files with google-java-format
Having a standard tool for formatting saves reviewers' valuable time. google-java-format is Google's standard formatter and is somewhat inspired by gofmt[1]. This commit formats everything using google-java-format version 1.2. The downside of this one-off formatting is breaking blame. This can be somewhat hacked around with a tool like git-hyper-blame[2], but it's definitely not optimal until/unless this kind of feature makes its way to git core. Not in this change: * Tool support, e.g. Eclipse. The command must be run manually [3]. * Documentation of best practice, e.g. new 100-column default. [1] https://talks.golang.org/2015/gofmt-en.slide#3 [2] https://commondatastorage.googleapis.com/chrome-infra-docs/flat/depot_tools/docs/html/git-hyper-blame.html [3] git ls-files | grep java$ | xargs google-java-format -i Change-Id: Id5f3c6de95ce0b68b41f0a478b5c99a93675aaa3 Signed-off-by: David Pursehouse <dpursehouse@collab.net>
This commit is contained in:

committed by
David Pursehouse

parent
6723b6d0fa
commit
292fa154c1
@@ -24,9 +24,7 @@ import com.google.gerrit.server.config.CanonicalWebUrl;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@@ -37,7 +35,8 @@ class OAuthLogoutServlet extends HttpLogoutServlet {
|
||||
private final Provider<OAuthSession> oauthSession;
|
||||
|
||||
@Inject
|
||||
OAuthLogoutServlet(AuthConfig authConfig,
|
||||
OAuthLogoutServlet(
|
||||
AuthConfig authConfig,
|
||||
DynamicItem<WebSession> webSession,
|
||||
@CanonicalWebUrl @Nullable Provider<String> urlProvider,
|
||||
AuditService audit,
|
||||
@@ -47,12 +46,10 @@ class OAuthLogoutServlet extends HttpLogoutServlet {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doLogout(HttpServletRequest req, HttpServletResponse rsp)
|
||||
throws IOException {
|
||||
protected void doLogout(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
|
||||
super.doLogout(req, rsp);
|
||||
if (req.getSession(false) != null) {
|
||||
oauthSession.get().logout();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -36,19 +36,16 @@ import com.google.gwtorm.server.OrmException;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
import com.google.inject.servlet.SessionScoped;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@SessionScoped
|
||||
/* OAuth protocol implementation */
|
||||
@@ -68,7 +65,8 @@ class OAuthSession {
|
||||
private boolean linkMode;
|
||||
|
||||
@Inject
|
||||
OAuthSession(DynamicItem<WebSession> webSession,
|
||||
OAuthSession(
|
||||
DynamicItem<WebSession> webSession,
|
||||
Provider<IdentifiedUser> identifiedUser,
|
||||
AccountManager accountManager,
|
||||
CanonicalWebUrl urlProvider,
|
||||
@@ -89,8 +87,9 @@ class OAuthSession {
|
||||
return Strings.emptyToNull(request.getParameter("code")) != null;
|
||||
}
|
||||
|
||||
boolean login(HttpServletRequest request, HttpServletResponse response,
|
||||
OAuthServiceProvider oauth) throws IOException {
|
||||
boolean login(
|
||||
HttpServletRequest request, HttpServletResponse response, OAuthServiceProvider oauth)
|
||||
throws IOException {
|
||||
log.debug("Login " + this);
|
||||
|
||||
if (isOAuthFinal(request)) {
|
||||
@@ -100,8 +99,7 @@ class OAuthSession {
|
||||
}
|
||||
|
||||
log.debug("Login-Retrieve-User " + this);
|
||||
OAuthToken token = oauth.getAccessToken(
|
||||
new OAuthVerifier(request.getParameter("code")));
|
||||
OAuthToken token = oauth.getAccessToken(new OAuthVerifier(request.getParameter("code")));
|
||||
user = oauth.getUserInfo(token);
|
||||
|
||||
if (isLoggedIn()) {
|
||||
@@ -119,22 +117,19 @@ class OAuthSession {
|
||||
// https://bz.apache.org/bugzilla/show_bug.cgi?id=28323
|
||||
// we cannot use LoginUrlToken.getToken() method,
|
||||
// because it relies on getPathInfo() and it is always null here.
|
||||
redirectToken = redirectToken.substring(
|
||||
request.getContextPath().length());
|
||||
response.sendRedirect(oauth.getAuthorizationUrl() +
|
||||
"&state=" + state);
|
||||
redirectToken = redirectToken.substring(request.getContextPath().length());
|
||||
response.sendRedirect(oauth.getAuthorizationUrl() + "&state=" + state);
|
||||
return false;
|
||||
}
|
||||
|
||||
private void authenticateAndRedirect(HttpServletRequest req,
|
||||
HttpServletResponse rsp, OAuthToken token) throws IOException {
|
||||
private void authenticateAndRedirect(
|
||||
HttpServletRequest req, HttpServletResponse rsp, OAuthToken token) throws IOException {
|
||||
AuthRequest areq = new AuthRequest(user.getExternalId());
|
||||
AuthResult arsp;
|
||||
try {
|
||||
String claimedIdentifier = user.getClaimedIdentity();
|
||||
if (!Strings.isNullOrEmpty(claimedIdentifier)) {
|
||||
if (!authenticateWithIdentityClaimedDuringHandshake(areq, rsp,
|
||||
claimedIdentifier)) {
|
||||
if (!authenticateWithIdentityClaimedDuringHandshake(areq, rsp, claimedIdentifier)) {
|
||||
return;
|
||||
}
|
||||
} else if (linkMode) {
|
||||
@@ -156,8 +151,7 @@ class OAuthSession {
|
||||
}
|
||||
|
||||
webSession.get().login(arsp, true);
|
||||
String suffix = redirectToken.substring(
|
||||
OAuthWebFilter.GERRIT_LOGIN.length() + 1);
|
||||
String suffix = redirectToken.substring(OAuthWebFilter.GERRIT_LOGIN.length() + 1);
|
||||
StringBuilder rdr = new StringBuilder(urlProvider.get(req));
|
||||
rdr.append(Url.decode(suffix));
|
||||
rsp.sendRedirect(rdr.toString());
|
||||
@@ -176,24 +170,35 @@ class OAuthSession {
|
||||
// This is (for now) a fatal error. There are two records
|
||||
// for what might be the same user.
|
||||
//
|
||||
log.error("OAuth accounts disagree over user identity:\n"
|
||||
+ " Claimed ID: " + claimedId.get() + " is " + claimedIdentifier
|
||||
+ "\n" + " Delgate ID: " + actualId.get() + " is "
|
||||
+ user.getExternalId());
|
||||
log.error(
|
||||
"OAuth accounts disagree over user identity:\n"
|
||||
+ " Claimed ID: "
|
||||
+ claimedId.get()
|
||||
+ " is "
|
||||
+ claimedIdentifier
|
||||
+ "\n"
|
||||
+ " Delgate ID: "
|
||||
+ actualId.get()
|
||||
+ " is "
|
||||
+ user.getExternalId());
|
||||
rsp.sendError(HttpServletResponse.SC_FORBIDDEN);
|
||||
return false;
|
||||
}
|
||||
} else if (claimedId.isPresent() && !actualId.isPresent()) {
|
||||
// Claimed account already exists: link to it.
|
||||
//
|
||||
log.info("OAuth2: linking claimed identity to {}",
|
||||
claimedId.get().toString());
|
||||
log.info("OAuth2: linking claimed identity to {}", claimedId.get().toString());
|
||||
try {
|
||||
accountManager.link(claimedId.get(), req);
|
||||
} catch (OrmException e) {
|
||||
log.error("Cannot link: " + user.getExternalId()
|
||||
+ " to user identity:\n"
|
||||
+ " Claimed ID: " + claimedId.get() + " is " + claimedIdentifier);
|
||||
log.error(
|
||||
"Cannot link: "
|
||||
+ user.getExternalId()
|
||||
+ " to user identity:\n"
|
||||
+ " Claimed ID: "
|
||||
+ claimedId.get()
|
||||
+ " is "
|
||||
+ claimedIdentifier);
|
||||
rsp.sendError(HttpServletResponse.SC_FORBIDDEN);
|
||||
return false;
|
||||
}
|
||||
@@ -201,13 +206,16 @@ class OAuthSession {
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean authenticateWithLinkedIdentity(AuthRequest areq,
|
||||
HttpServletResponse rsp) throws AccountException, IOException {
|
||||
private boolean authenticateWithLinkedIdentity(AuthRequest areq, HttpServletResponse rsp)
|
||||
throws AccountException, IOException {
|
||||
try {
|
||||
accountManager.link(identifiedUser.get().getAccountId(), areq);
|
||||
} catch (OrmException e) {
|
||||
log.error("Cannot link: " + user.getExternalId()
|
||||
+ " to user identity: " + identifiedUser.get().getAccountId());
|
||||
log.error(
|
||||
"Cannot link: "
|
||||
+ user.getExternalId()
|
||||
+ " to user identity: "
|
||||
+ identifiedUser.get().getAccountId());
|
||||
rsp.sendError(HttpServletResponse.SC_FORBIDDEN);
|
||||
return false;
|
||||
} finally {
|
||||
@@ -239,8 +247,7 @@ class OAuthSession {
|
||||
try {
|
||||
return SecureRandom.getInstance("SHA1PRNG");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new IllegalArgumentException(
|
||||
"No SecureRandom available for GitHub authentication", e);
|
||||
throw new IllegalArgumentException("No SecureRandom available for GitHub authentication", e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -252,8 +259,7 @@ class OAuthSession {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "OAuthSession [token=" + tokenCache.get(accountId) + ", user="
|
||||
+ user + "]";
|
||||
return "OAuthSession [token=" + tokenCache.get(accountId) + ", user=" + user + "]";
|
||||
}
|
||||
|
||||
public void setServiceProvider(OAuthServiceProvider provider) {
|
||||
|
@@ -29,16 +29,11 @@ import com.google.gerrit.server.config.CanonicalWebUrl;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.SortedMap;
|
||||
import java.util.SortedSet;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
@@ -48,6 +43,8 @@ import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
@Singleton
|
||||
/* OAuth web filter uses active OAuth session to perform OAuth requests */
|
||||
@@ -61,7 +58,8 @@ class OAuthWebFilter implements Filter {
|
||||
private OAuthServiceProvider ssoProvider;
|
||||
|
||||
@Inject
|
||||
OAuthWebFilter(@CanonicalWebUrl @Nullable Provider<String> urlProvider,
|
||||
OAuthWebFilter(
|
||||
@CanonicalWebUrl @Nullable Provider<String> urlProvider,
|
||||
DynamicMap<OAuthServiceProvider> oauthServiceProviders,
|
||||
Provider<OAuthSession> oauthSessionProvider,
|
||||
SiteHeaderFooter header) {
|
||||
@@ -77,12 +75,11 @@ class OAuthWebFilter implements Filter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
}
|
||||
public void destroy() {}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response,
|
||||
FilterChain chain) throws IOException, ServletException {
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
||||
throws IOException, ServletException {
|
||||
HttpServletRequest httpRequest = (HttpServletRequest) request;
|
||||
HttpServletResponse httpResponse = (HttpServletResponse) response;
|
||||
|
||||
@@ -93,9 +90,8 @@ class OAuthWebFilter implements Filter {
|
||||
}
|
||||
|
||||
String provider = httpRequest.getParameter("provider");
|
||||
OAuthServiceProvider service = ssoProvider == null
|
||||
? oauthSession.getServiceProvider()
|
||||
: ssoProvider;
|
||||
OAuthServiceProvider service =
|
||||
ssoProvider == null ? oauthSession.getServiceProvider() : ssoProvider;
|
||||
|
||||
if (isGerritLogin(httpRequest) || oauthSession.isOAuthFinal(httpRequest)) {
|
||||
if (service == null && Strings.isNullOrEmpty(provider)) {
|
||||
@@ -112,29 +108,24 @@ class OAuthWebFilter implements Filter {
|
||||
}
|
||||
}
|
||||
|
||||
private OAuthServiceProvider findService(String providerId)
|
||||
throws ServletException {
|
||||
private OAuthServiceProvider findService(String providerId) throws ServletException {
|
||||
Set<String> plugins = oauthServiceProviders.plugins();
|
||||
for (String pluginName : plugins) {
|
||||
Map<String, Provider<OAuthServiceProvider>> m =
|
||||
oauthServiceProviders.byPlugin(pluginName);
|
||||
for (Map.Entry<String, Provider<OAuthServiceProvider>> e
|
||||
: m.entrySet()) {
|
||||
if (providerId.equals(
|
||||
String.format("%s_%s", pluginName, e.getKey()))) {
|
||||
return e.getValue().get();
|
||||
}
|
||||
Map<String, Provider<OAuthServiceProvider>> m = oauthServiceProviders.byPlugin(pluginName);
|
||||
for (Map.Entry<String, Provider<OAuthServiceProvider>> e : m.entrySet()) {
|
||||
if (providerId.equals(String.format("%s_%s", pluginName, e.getKey()))) {
|
||||
return e.getValue().get();
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new ServletException("No provider found for: " + providerId);
|
||||
}
|
||||
|
||||
private void selectProvider(HttpServletRequest req, HttpServletResponse res,
|
||||
@Nullable String errorMessage)
|
||||
private void selectProvider(
|
||||
HttpServletRequest req, HttpServletResponse res, @Nullable String errorMessage)
|
||||
throws IOException {
|
||||
String self = req.getRequestURI();
|
||||
String cancel = MoreObjects.firstNonNull(
|
||||
urlProvider != null ? urlProvider.get() : "/", "/");
|
||||
String cancel = MoreObjects.firstNonNull(urlProvider != null ? urlProvider.get() : "/", "/");
|
||||
cancel += LoginUrlToken.getToken(req);
|
||||
|
||||
Document doc = header.parse(OAuthWebFilter.class, "LoginForm.html");
|
||||
@@ -153,33 +144,26 @@ class OAuthWebFilter implements Filter {
|
||||
|
||||
Set<String> plugins = oauthServiceProviders.plugins();
|
||||
for (String pluginName : plugins) {
|
||||
Map<String, Provider<OAuthServiceProvider>> m =
|
||||
oauthServiceProviders.byPlugin(pluginName);
|
||||
for (Map.Entry<String, Provider<OAuthServiceProvider>> e
|
||||
: m.entrySet()) {
|
||||
addProvider(providers, pluginName, e.getKey(),
|
||||
e.getValue().get().getName());
|
||||
}
|
||||
Map<String, Provider<OAuthServiceProvider>> m = oauthServiceProviders.byPlugin(pluginName);
|
||||
for (Map.Entry<String, Provider<OAuthServiceProvider>> e : m.entrySet()) {
|
||||
addProvider(providers, pluginName, e.getKey(), e.getValue().get().getName());
|
||||
}
|
||||
}
|
||||
|
||||
sendHtml(res, doc);
|
||||
}
|
||||
|
||||
private static void addProvider(Element form, String pluginName,
|
||||
String id, String serviceName) {
|
||||
private static void addProvider(Element form, String pluginName, String id, String serviceName) {
|
||||
Element div = form.getOwnerDocument().createElement("div");
|
||||
div.setAttribute("id", id);
|
||||
Element hyperlink = form.getOwnerDocument().createElement("a");
|
||||
hyperlink.setAttribute("href", String.format("?provider=%s_%s",
|
||||
pluginName, id));
|
||||
hyperlink.setTextContent(serviceName +
|
||||
" (" + pluginName + " plugin)");
|
||||
hyperlink.setAttribute("href", String.format("?provider=%s_%s", pluginName, id));
|
||||
hyperlink.setTextContent(serviceName + " (" + pluginName + " plugin)");
|
||||
div.appendChild(hyperlink);
|
||||
form.appendChild(div);
|
||||
}
|
||||
|
||||
private static void sendHtml(HttpServletResponse res, Document doc)
|
||||
throws IOException {
|
||||
private static void sendHtml(HttpServletResponse res, Document doc) throws IOException {
|
||||
byte[] bin = HtmlDomUtil.toUTF8(doc);
|
||||
res.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
||||
res.setContentType("text/html");
|
||||
@@ -190,12 +174,10 @@ class OAuthWebFilter implements Filter {
|
||||
}
|
||||
}
|
||||
|
||||
private void pickSSOServiceProvider()
|
||||
throws ServletException {
|
||||
private void pickSSOServiceProvider() throws ServletException {
|
||||
SortedSet<String> plugins = oauthServiceProviders.plugins();
|
||||
if (plugins.isEmpty()) {
|
||||
throw new ServletException(
|
||||
"OAuth service provider wasn't installed");
|
||||
throw new ServletException("OAuth service provider wasn't installed");
|
||||
}
|
||||
if (plugins.size() == 1) {
|
||||
SortedMap<String, Provider<OAuthServiceProvider>> services =
|
||||
|
Reference in New Issue
Block a user