Files
gerrit/java/com/google/gerrit/httpd/DirectChangeByCommit.java
Edwin Kempin 03d2d209b1 Migrate httpd classes to Flogger
This is the first part of the migration to Flogger. This change
migrates all classes of the 'http' module to Flogger. Other modules
continue to use slf4j. They should be migrated by follow-up changes.

During this migration we try to make the log statements more consistent:
- avoid string concatenation
- avoid usage of String.format(...)

Change-Id: I473c41733b00aa1ceab92fe0dc8cd1c6b347174c
Signed-off-by: Edwin Kempin <ekempin@google.com>
2018-06-05 13:06:05 +02:00

58 lines
2.0 KiB
Java

// Copyright 2011 Google Inc. All Rights Reserved.
package com.google.gerrit.httpd;
import com.google.common.base.CharMatcher;
import com.google.common.collect.ImmutableList;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.common.PageLinks;
import com.google.gerrit.extensions.api.changes.Changes;
import com.google.gerrit.extensions.common.ChangeInfo;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.Project;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Singleton
class DirectChangeByCommit extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private final Changes changes;
@Inject
DirectChangeByCommit(Changes changes) {
this.changes = changes;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
String query = CharMatcher.is('/').trimTrailingFrom(req.getPathInfo());
List<ChangeInfo> results;
try {
results = changes.query(query).withLimit(2).get();
} catch (RestApiException e) {
logger.atWarning().withCause(e).log("Cannot process query by URL: /r/%s", query);
results = ImmutableList.of();
}
String token;
if (results.size() == 1) {
// If exactly one change matches, link to that change.
// TODO Link to a specific patch set, if one matched.
ChangeInfo ci = results.iterator().next();
token = PageLinks.toChange(new Project.NameKey(ci.project), new Change.Id(ci._number));
} else {
// Otherwise, link to the query page.
token = PageLinks.toChangeQuery(query);
}
UrlModule.toGerrit(token, req, rsp);
}
}