Use Guice to setup the FileTypeRegistery singleton
This avoids the static singleton. Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
@@ -47,8 +47,6 @@ import java.security.SecureRandom;
|
|||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipOutputStream;
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
import javax.servlet.ServletConfig;
|
|
||||||
import javax.servlet.ServletException;
|
|
||||||
import javax.servlet.http.HttpServlet;
|
import javax.servlet.http.HttpServlet;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
@@ -68,18 +66,13 @@ public class CatServlet extends HttpServlet {
|
|||||||
private static final MimeType ZIP = new MimeType("application/zip");
|
private static final MimeType ZIP = new MimeType("application/zip");
|
||||||
private final GerritServer server;
|
private final GerritServer server;
|
||||||
private final SecureRandom rng;
|
private final SecureRandom rng;
|
||||||
private FileTypeRegistry registry;
|
private final FileTypeRegistry registry;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
CatServlet(final GerritServer gs) {
|
CatServlet(final GerritServer gs, final FileTypeRegistry ftr) {
|
||||||
server = gs;
|
server = gs;
|
||||||
rng = new SecureRandom();
|
rng = new SecureRandom();
|
||||||
}
|
registry = ftr;
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init(final ServletConfig config) throws ServletException {
|
|
||||||
super.init(config);
|
|
||||||
registry = FileTypeRegistry.getInstance();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -14,8 +14,8 @@
|
|||||||
|
|
||||||
package com.google.gerrit.server;
|
package com.google.gerrit.server;
|
||||||
|
|
||||||
import com.google.gwtjsonrpc.server.XsrfException;
|
import com.google.inject.Inject;
|
||||||
import com.google.gwtorm.client.OrmException;
|
import com.google.inject.Singleton;
|
||||||
|
|
||||||
import eu.medsea.mimeutil.MimeException;
|
import eu.medsea.mimeutil.MimeException;
|
||||||
import eu.medsea.mimeutil.MimeType;
|
import eu.medsea.mimeutil.MimeType;
|
||||||
@@ -35,21 +35,19 @@ import java.util.HashSet;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
@Singleton
|
||||||
public class FileTypeRegistry {
|
public class FileTypeRegistry {
|
||||||
private static final String KEY_SAFE = "safe";
|
private static final String KEY_SAFE = "safe";
|
||||||
private static final String SECTION_MIMETYPE = "mimetype";
|
private static final String SECTION_MIMETYPE = "mimetype";
|
||||||
private static final Logger log =
|
private static final Logger log =
|
||||||
LoggerFactory.getLogger(FileTypeRegistry.class);
|
LoggerFactory.getLogger(FileTypeRegistry.class);
|
||||||
private static final FileTypeRegistry INSTANCE = new FileTypeRegistry();
|
|
||||||
|
|
||||||
/** Get the global registry. */
|
|
||||||
public static FileTypeRegistry getInstance() {
|
|
||||||
return INSTANCE;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
private final GerritServer server;
|
||||||
private MimeUtil2 mimeUtil;
|
private MimeUtil2 mimeUtil;
|
||||||
|
|
||||||
private FileTypeRegistry() {
|
@Inject
|
||||||
|
FileTypeRegistry(final GerritServer gs) {
|
||||||
|
server = gs;
|
||||||
mimeUtil = new MimeUtil2();
|
mimeUtil = new MimeUtil2();
|
||||||
register("eu.medsea.mimeutil.detector.ExtensionMimeDetector");
|
register("eu.medsea.mimeutil.detector.ExtensionMimeDetector");
|
||||||
register("eu.medsea.mimeutil.detector.MagicMimeMimeDetector");
|
register("eu.medsea.mimeutil.detector.MagicMimeMimeDetector");
|
||||||
@@ -133,34 +131,16 @@ public class FileTypeRegistry {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
final RepositoryConfig cfg = getGerritConfig();
|
final RepositoryConfig cfg = server.getGerritConfig();
|
||||||
if (cfg != null) {
|
final boolean any = isSafe(cfg, "*/*", false);
|
||||||
final boolean any = isSafe(cfg, "*/*", false);
|
final boolean genericMedia = isSafe(cfg, type.getMediaType() + "/*", any);
|
||||||
final boolean genericMedia = isSafe(cfg, type.getMediaType() + "/*", any);
|
return isSafe(cfg, type.toString(), genericMedia);
|
||||||
return isSafe(cfg, type.toString(), genericMedia);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Assume we cannot send the content inline.
|
|
||||||
//
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isSafe(RepositoryConfig cfg, String type, boolean def) {
|
private static boolean isSafe(RepositoryConfig cfg, String type, boolean def) {
|
||||||
return cfg.getBoolean(SECTION_MIMETYPE, type, KEY_SAFE, def);
|
return cfg.getBoolean(SECTION_MIMETYPE, type, KEY_SAFE, def);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static RepositoryConfig getGerritConfig() {
|
|
||||||
try {
|
|
||||||
return GerritServer.getInstance().getGerritConfig();
|
|
||||||
} catch (OrmException e) {
|
|
||||||
log.warn("Cannot obtain GerritServer", e);
|
|
||||||
return null;
|
|
||||||
} catch (XsrfException e) {
|
|
||||||
log.warn("Cannot obtain GerritServer", e);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean isUnknownType(Collection<MimeType> mimeTypes) {
|
private static boolean isUnknownType(Collection<MimeType> mimeTypes) {
|
||||||
if (mimeTypes.isEmpty()) {
|
if (mimeTypes.isEmpty()) {
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -90,6 +90,7 @@ public class GerritServletConfig extends GuiceServletContextListener {
|
|||||||
bind(GerritServer.class).toInstance(gs);
|
bind(GerritServer.class).toInstance(gs);
|
||||||
bind(ContactStore.class).toInstance(EncryptedContactStore.create(gs));
|
bind(ContactStore.class).toInstance(EncryptedContactStore.create(gs));
|
||||||
bind(OpenIdServiceImpl.class).toInstance(new OpenIdServiceImpl(gs));
|
bind(OpenIdServiceImpl.class).toInstance(new OpenIdServiceImpl(gs));
|
||||||
|
bind(FileTypeRegistry.class).toInstance(new FileTypeRegistry(gs));
|
||||||
} catch (OrmException e) {
|
} catch (OrmException e) {
|
||||||
addError(e);
|
addError(e);
|
||||||
} catch (XsrfException e) {
|
} catch (XsrfException e) {
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ import com.google.gerrit.client.rpc.NoSuchAccountException;
|
|||||||
import com.google.gerrit.client.rpc.NoSuchEntityException;
|
import com.google.gerrit.client.rpc.NoSuchEntityException;
|
||||||
import com.google.gerrit.server.BaseServiceImplementation;
|
import com.google.gerrit.server.BaseServiceImplementation;
|
||||||
import com.google.gerrit.server.ChangeUtil;
|
import com.google.gerrit.server.ChangeUtil;
|
||||||
|
import com.google.gerrit.server.FileTypeRegistry;
|
||||||
import com.google.gerrit.server.GerritJsonServlet;
|
import com.google.gerrit.server.GerritJsonServlet;
|
||||||
import com.google.gerrit.server.GerritServer;
|
import com.google.gerrit.server.GerritServer;
|
||||||
import com.google.gerrit.server.mail.AbandonedSender;
|
import com.google.gerrit.server.mail.AbandonedSender;
|
||||||
@@ -65,9 +66,11 @@ import java.util.Set;
|
|||||||
class PatchDetailServiceImpl extends BaseServiceImplementation implements
|
class PatchDetailServiceImpl extends BaseServiceImplementation implements
|
||||||
PatchDetailService {
|
PatchDetailService {
|
||||||
private final Logger log = LoggerFactory.getLogger(getClass());
|
private final Logger log = LoggerFactory.getLogger(getClass());
|
||||||
|
private final FileTypeRegistry registry;
|
||||||
|
|
||||||
PatchDetailServiceImpl(final GerritServer gs) {
|
PatchDetailServiceImpl(final GerritServer gs, final FileTypeRegistry ftr) {
|
||||||
super(gs);
|
super(gs);
|
||||||
|
registry = ftr;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void patchScript(final Patch.Key patchKey, final PatchSet.Id psa,
|
public void patchScript(final Patch.Key patchKey, final PatchSet.Id psa,
|
||||||
@@ -77,7 +80,8 @@ class PatchDetailServiceImpl extends BaseServiceImplementation implements
|
|||||||
callback.onFailure(new NoSuchEntityException());
|
callback.onFailure(new NoSuchEntityException());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
run(callback, new PatchScriptAction(server, patchKey, psa, psb, s));
|
run(callback,
|
||||||
|
new PatchScriptAction(server, registry, patchKey, psa, psb, s));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void patchComments(final Patch.Key patchKey, final PatchSet.Id psa,
|
public void patchComments(final Patch.Key patchKey, final PatchSet.Id psa,
|
||||||
@@ -109,7 +113,8 @@ class PatchDetailServiceImpl extends BaseServiceImplementation implements
|
|||||||
if (comment.getKey().get() == null) {
|
if (comment.getKey().get() == null) {
|
||||||
final PatchLineComment nc =
|
final PatchLineComment nc =
|
||||||
new PatchLineComment(new PatchLineComment.Key(patch.getKey(),
|
new PatchLineComment(new PatchLineComment.Key(patch.getKey(),
|
||||||
ChangeUtil.messageUUID(db)), comment.getLine(), me, comment.getParentUuid());
|
ChangeUtil.messageUUID(db)), comment.getLine(), me, comment
|
||||||
|
.getParentUuid());
|
||||||
nc.setSide(comment.getSide());
|
nc.setSide(comment.getSide());
|
||||||
nc.setMessage(comment.getMessage());
|
nc.setMessage(comment.getMessage());
|
||||||
db.patchComments().insert(Collections.singleton(nc));
|
db.patchComments().insert(Collections.singleton(nc));
|
||||||
@@ -183,16 +188,17 @@ class PatchDetailServiceImpl extends BaseServiceImplementation implements
|
|||||||
/**
|
/**
|
||||||
* Update the reviewed status for the file by user @code{account}
|
* Update the reviewed status for the file by user @code{account}
|
||||||
*/
|
*/
|
||||||
public void setReviewedByCurrentUser(final Key patchKey, final boolean reviewed,
|
public void setReviewedByCurrentUser(final Key patchKey,
|
||||||
AsyncCallback<VoidResult> callback) {
|
final boolean reviewed, AsyncCallback<VoidResult> callback) {
|
||||||
run(callback, new Action<VoidResult>() {
|
run(callback, new Action<VoidResult>() {
|
||||||
public VoidResult run(ReviewDb db) throws OrmException {
|
public VoidResult run(ReviewDb db) throws OrmException {
|
||||||
Account.Id account = Common.getAccountId();
|
Account.Id account = Common.getAccountId();
|
||||||
AccountPatchReview.Key key = new AccountPatchReview.Key(patchKey, account);
|
AccountPatchReview.Key key =
|
||||||
|
new AccountPatchReview.Key(patchKey, account);
|
||||||
AccountPatchReview apr = db.accountPatchReviews().get(key);
|
AccountPatchReview apr = db.accountPatchReviews().get(key);
|
||||||
if (apr == null && reviewed) {
|
if (apr == null && reviewed) {
|
||||||
db.accountPatchReviews().
|
db.accountPatchReviews().insert(
|
||||||
insert(Collections.singleton(new AccountPatchReview(patchKey, account)));
|
Collections.singleton(new AccountPatchReview(patchKey, account)));
|
||||||
} else if (apr != null && !reviewed) {
|
} else if (apr != null && !reviewed) {
|
||||||
db.accountPatchReviews().delete(Collections.singleton(apr));
|
db.accountPatchReviews().delete(Collections.singleton(apr));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
package com.google.gerrit.server.patch;
|
package com.google.gerrit.server.patch;
|
||||||
|
|
||||||
|
import com.google.gerrit.server.FileTypeRegistry;
|
||||||
import com.google.gerrit.server.GerritJsonServlet;
|
import com.google.gerrit.server.GerritJsonServlet;
|
||||||
import com.google.gerrit.server.GerritServer;
|
import com.google.gerrit.server.GerritServer;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
@@ -23,13 +24,16 @@ import com.google.inject.Singleton;
|
|||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
@Singleton
|
@Singleton
|
||||||
public class PatchDetailServiceSrv extends GerritJsonServlet {
|
public class PatchDetailServiceSrv extends GerritJsonServlet {
|
||||||
|
private final FileTypeRegistry registry;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
PatchDetailServiceSrv(final GerritServer gs) {
|
PatchDetailServiceSrv(final GerritServer gs, final FileTypeRegistry ftr) {
|
||||||
super(gs);
|
super(gs);
|
||||||
|
registry = ftr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Object createServiceHandle() throws Exception {
|
protected Object createServiceHandle() throws Exception {
|
||||||
return new PatchDetailServiceImpl(server);
|
return new PatchDetailServiceImpl(server, registry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ import com.google.gerrit.client.reviewdb.ReviewDb;
|
|||||||
import com.google.gerrit.client.rpc.Common;
|
import com.google.gerrit.client.rpc.Common;
|
||||||
import com.google.gerrit.client.rpc.CorruptEntityException;
|
import com.google.gerrit.client.rpc.CorruptEntityException;
|
||||||
import com.google.gerrit.client.rpc.NoSuchEntityException;
|
import com.google.gerrit.client.rpc.NoSuchEntityException;
|
||||||
|
import com.google.gerrit.server.FileTypeRegistry;
|
||||||
import com.google.gerrit.server.GerritServer;
|
import com.google.gerrit.server.GerritServer;
|
||||||
import com.google.gerrit.server.BaseServiceImplementation.Action;
|
import com.google.gerrit.server.BaseServiceImplementation.Action;
|
||||||
import com.google.gerrit.server.BaseServiceImplementation.Failure;
|
import com.google.gerrit.server.BaseServiceImplementation.Failure;
|
||||||
@@ -56,6 +57,7 @@ class PatchScriptAction implements Action<PatchScript> {
|
|||||||
LoggerFactory.getLogger(PatchScriptAction.class);
|
LoggerFactory.getLogger(PatchScriptAction.class);
|
||||||
|
|
||||||
private final GerritServer server;
|
private final GerritServer server;
|
||||||
|
private final FileTypeRegistry registry;
|
||||||
private final Patch.Key patchKey;
|
private final Patch.Key patchKey;
|
||||||
private final PatchSet.Id psa;
|
private final PatchSet.Id psa;
|
||||||
private final PatchSet.Id psb;
|
private final PatchSet.Id psb;
|
||||||
@@ -69,10 +71,11 @@ class PatchScriptAction implements Action<PatchScript> {
|
|||||||
private Project.NameKey projectKey;
|
private Project.NameKey projectKey;
|
||||||
private Repository git;
|
private Repository git;
|
||||||
|
|
||||||
PatchScriptAction(final GerritServer gs, final Patch.Key patchKey,
|
PatchScriptAction(final GerritServer gs, final FileTypeRegistry ftr,
|
||||||
final PatchSet.Id psa, final PatchSet.Id psb,
|
final Patch.Key patchKey, final PatchSet.Id psa, final PatchSet.Id psb,
|
||||||
final PatchScriptSettings settings) {
|
final PatchScriptSettings settings) {
|
||||||
this.server = gs;
|
this.server = gs;
|
||||||
|
this.registry = ftr;
|
||||||
this.patchKey = patchKey;
|
this.patchKey = patchKey;
|
||||||
this.psa = psa;
|
this.psa = psa;
|
||||||
this.psb = psb;
|
this.psb = psb;
|
||||||
@@ -167,7 +170,7 @@ class PatchScriptAction implements Action<PatchScript> {
|
|||||||
else
|
else
|
||||||
throw new Failure(new NoSuchEntityException());
|
throw new Failure(new NoSuchEntityException());
|
||||||
|
|
||||||
final PatchScriptBuilder b = new PatchScriptBuilder();
|
final PatchScriptBuilder b = new PatchScriptBuilder(registry);
|
||||||
b.setRepository(git);
|
b.setRepository(git);
|
||||||
b.setPatch(patch);
|
b.setPatch(patch);
|
||||||
b.setSettings(s);
|
b.setSettings(s);
|
||||||
|
|||||||
@@ -78,11 +78,11 @@ class PatchScriptBuilder {
|
|||||||
private List<Edit> edits;
|
private List<Edit> edits;
|
||||||
private final FileTypeRegistry registry;
|
private final FileTypeRegistry registry;
|
||||||
|
|
||||||
PatchScriptBuilder() {
|
PatchScriptBuilder(final FileTypeRegistry ftr) {
|
||||||
header = new ArrayList<String>();
|
header = new ArrayList<String>();
|
||||||
a = new Side();
|
a = new Side();
|
||||||
b = new Side();
|
b = new Side();
|
||||||
registry = FileTypeRegistry.getInstance();
|
registry = ftr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setRepository(final Repository r) {
|
void setRepository(final Repository r) {
|
||||||
|
|||||||
Reference in New Issue
Block a user