Skip to content

The Unified and Autonomous Development Platform

Refactoring gravatars

robin shineb3a0ec5be773
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/manager/UserManager.java b/gitop.core/src/main/java/com/pmease/gitop/core/manager/UserManager.java
index 28fcaefb0e..04c2792700 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/manager/UserManager.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/manager/UserManager.java
@@ -62,4 +62,5 @@ public interface UserManager extends GenericDao<User> {
* @return all accounts can be managed
*/
List<User> getManagableAccounts(User user);
+
}
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/setting/SystemSetting.java b/gitop.core/src/main/java/com/pmease/gitop/core/setting/SystemSetting.java
index ad071a7869..a91b7083c4 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/setting/SystemSetting.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/setting/SystemSetting.java
@@ -23,6 +23,8 @@ public class SystemSetting implements Serializable, Validatable {
private GitConfig gitConfig = new SystemGit();
+ private boolean gravatarEnabled;
+
@Editable(name="Directory to Store Application Data", order=100, description="Specify directory to store application data, "
+ "such as managed git repositories and various settings.")
@Directory
@@ -47,6 +49,15 @@ public class SystemSetting implements Serializable, Validatable {
this.gitConfig = gitConfig;
}
+ @Editable(order=300, description="Whether or not to enable user gravatar.")
+ public boolean isGravatarEnabled() {
+ return gravatarEnabled;
+ }
+
+ public void setGravatarEnabled(boolean gravatarEnabled) {
+ this.gravatarEnabled = gravatarEnabled;
+ }
+
@Override
public void validate(ConstraintValidatorContext constraintValidatorContext) {
File dataDir = new File(dataPath);
diff --git a/gitop.model/src/main/java/com/pmease/gitop/model/User.java b/gitop.model/src/main/java/com/pmease/gitop/model/User.java
index d944f09d40..97110206ce 100644
--- a/gitop.model/src/main/java/com/pmease/gitop/model/User.java
+++ b/gitop.model/src/main/java/com/pmease/gitop/model/User.java
@@ -1,5 +1,6 @@
package com.pmease.gitop.model;
+import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
@@ -12,6 +13,7 @@ import org.eclipse.jgit.lib.PersonIdent;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
+import com.pmease.commons.bootstrap.Bootstrap;
import com.pmease.commons.editable.annotation.Editable;
import com.pmease.commons.editable.annotation.Password;
import com.pmease.commons.shiro.AbstractUser;
@@ -29,8 +31,6 @@ public class User extends AbstractUser implements ProtectedObject {
private String displayName;
- private String avatarUrl;
-
private boolean admin;
@OneToMany(mappedBy="user", cascade=CascadeType.REMOVE)
@@ -103,14 +103,6 @@ public class User extends AbstractUser implements ProtectedObject {
this.admin = admin;
}
- public String getAvatarUrl() {
- return avatarUrl;
- }
-
- public void setAvatarUrl(String avatarUrl) {
- this.avatarUrl = avatarUrl;
- }
-
public Collection<Membership> getMemberships() {
return memberships;
}
@@ -235,5 +227,9 @@ public class User extends AbstractUser implements ProtectedObject {
public PersonIdent asPerson() {
return new PersonIdent(getName(), getEmail());
}
+
+ public File getLocalAvatar() {
+ return new File(Bootstrap.installDir, "avatars/" + getId());
+ }
}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/GitopWebApp.java b/gitop.web/src/main/java/com/pmease/gitop/web/GitopWebApp.java
index 7ef0bf7c6b..862b76aa09 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/GitopWebApp.java
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/GitopWebApp.java
@@ -177,14 +177,6 @@ public class GitopWebApp extends AbstractWicketConfig {
}
}
- public boolean isGravatarEnabled() {
- return true;
- }
-
- public boolean isPublicSignupEnabled() {
- return true;
- }
-
public Iterable<IRequestMapper> getRequestMappers() {
return getRootRequestMapperAsCompound();
}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/SitePaths.java b/gitop.web/src/main/java/com/pmease/gitop/web/SitePaths.java
deleted file mode 100644
index d1425dbe17..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/SitePaths.java
+++ /dev/null
@@ -1,55 +0,0 @@
-package com.pmease.gitop.web;
-
-import static com.google.common.base.Preconditions.checkNotNull;
-
-import java.io.File;
-
-import javax.inject.Inject;
-import javax.inject.Singleton;
-
-import com.pmease.commons.bootstrap.Bootstrap;
-import com.pmease.commons.loader.AppLoader;
-import com.pmease.gitop.core.manager.ConfigManager;
-import com.pmease.gitop.model.User;
-
-@Singleton
-public class SitePaths {
- private final ConfigManager configManager;
-
- public static SitePaths get() {
- return AppLoader.getInstance(SitePaths.class);
- }
-
- @Inject
- SitePaths(ConfigManager configManager) {
- this.configManager = configManager;
- }
-
- public File installDir() {
- return Bootstrap.installDir;
- }
-
- public File dataDir() {
- return new File(configManager.getSystemSetting().getDataPath());
- }
-
- public File avatarsDir() {
- return new File(dataDir(), "avatars");
- }
-
- public File userAvatarDir(Long id) {
- return new File(avatarsDir(), "users/" + checkNotNull(id));
- }
-
- public File userAvatarDir(User user) {
- return userAvatarDir(checkNotNull(user).getId());
- }
-
- public File tempDir() {
- return new File(installDir(), "temp");
- }
-
- public File uploadsDir() {
- return new File(dataDir(), "uploads");
- }
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/WebModule.java b/gitop.web/src/main/java/com/pmease/gitop/web/WebModule.java
index 5b6780a176..53736efc97 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/WebModule.java
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/WebModule.java
@@ -1,8 +1,5 @@
package com.pmease.gitop.web;
-import javax.inject.Singleton;
-
-import com.google.common.collect.ImmutableSet;
import com.pmease.commons.editable.EditSupport;
import com.pmease.commons.jersey.JerseyConfigurator;
import com.pmease.commons.jersey.JerseyEnvironment;
@@ -10,10 +7,9 @@ import com.pmease.commons.jetty.ServletConfigurator;
import com.pmease.commons.loader.AbstractPluginModule;
import com.pmease.commons.wicket.AbstractWicketConfig;
import com.pmease.gitop.model.validation.UserNameReservation;
-import com.pmease.gitop.web.common.soy.impl.SoyTemplateModule;
import com.pmease.gitop.web.editable.EditSupportLocator;
import com.pmease.gitop.web.page.repository.source.blob.renderer.BlobRendererFactory;
-import com.pmease.gitop.web.resource.TestResource;
+import com.pmease.gitop.web.resource.ResourceLocator;
import com.pmease.gitop.web.service.ServiceModule;
/**
@@ -28,7 +24,6 @@ public class WebModule extends AbstractPluginModule {
// put your guice bindings here
bind(AbstractWicketConfig.class).to(GitopWebApp.class);
- bind(SitePaths.class).in(Singleton.class);
contribute(ServletConfigurator.class, WebServletConfigurator.class);
contribute(UserNameReservation.class, WebUserNameReservation.class);
@@ -36,15 +31,13 @@ public class WebModule extends AbstractPluginModule {
@Override
public void configure(JerseyEnvironment environment) {
- environment.addComponentFromPackage(TestResource.class);
+ environment.addComponentFromPackage(ResourceLocator.class);
}
});
contributeFromPackage(EditSupport.class, EditSupportLocator.class);
- install(new SoyTemplateModule(ImmutableSet.<String>of(GitopWebApp.class.getPackage().getName())));
-
install(new ServiceModule());
bind(BlobRendererFactory.class);
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/quantity/Data.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/quantity/Data.java
index 88bd3b5f8f..6c797fe9e9 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/quantity/Data.java
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/common/quantity/Data.java
@@ -2,6 +2,7 @@ package com.pmease.gitop.web.common.quantity;
import com.pmease.gitop.web.common.datatype.DataTypes;
+
/**
* Provides a unit to allow conversions and unambiguous passing around of data
* {@link Amount}s. The kilo/mega/giga/... hierarchy is built on base 2 so that
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/SoyPanel.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/SoyPanel.java
deleted file mode 100644
index e7b9e60216..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/SoyPanel.java
+++ /dev/null
@@ -1,70 +0,0 @@
-package com.pmease.gitop.web.common.soy;
-
-import java.util.Map;
-
-import org.apache.wicket.MarkupContainer;
-import org.apache.wicket.markup.IMarkupCacheKeyProvider;
-import org.apache.wicket.markup.IMarkupResourceStreamProvider;
-import org.apache.wicket.markup.html.panel.Panel;
-import org.apache.wicket.model.IModel;
-import org.apache.wicket.util.resource.IResourceStream;
-import org.apache.wicket.util.resource.StringResourceStream;
-
-import com.google.common.collect.Maps;
-import com.pmease.gitop.core.Gitop;
-import com.pmease.gitop.web.common.soy.api.SoyTemplateRenderer;
-import com.pmease.gitop.web.common.soy.api.TemplateKey;
-
-public class SoyPanel extends Panel implements IMarkupResourceStreamProvider,
- IMarkupCacheKeyProvider {
-
- private static final long serialVersionUID = 1L;
-
- private final TemplateKey templateKey;
-
- public SoyPanel(String id, TemplateKey key) {
- this(id, key, null);
- }
-
- public SoyPanel(String id, TemplateKey key, IModel<Map<String, Object>> data) {
- super(id, data);
- this.templateKey = key;
- }
-
- @Override
- public IResourceStream getMarkupResourceStream(MarkupContainer container,
- Class<?> containerClass) {
-// Stopwatch watch = new Stopwatch().start();
- StringBuffer sb = new StringBuffer();
- sb.append("<wicket:panel>");
- sb.append(evaluateSoyTemplate());
- sb.append("</wicket:panel>");
-// System.out.println("Duration " + watch.elapsedMillis());
- return new StringResourceStream(sb.toString());
- }
-
- @SuppressWarnings("unchecked")
- protected Map<String, ?> getData() {
- if (getDefaultModel() == null) {
- return Maps.newHashMap();
- }
-
- return (Map<String, ?>) getDefaultModelObject();
- }
-
- protected Map<String, ?> getIjData() {
- return Maps.newHashMap();
- }
-
- protected String evaluateSoyTemplate() {
- SoyTemplateRenderer renderer = Gitop.getInstance(SoyTemplateRenderer.class);
- StringBuffer sb = new StringBuffer();
- renderer.render(templateKey, sb, getData(), getIjData());
- return sb.toString();
- }
-
- @Override
- public String getCacheKey(MarkupContainer container, Class<?> containerClass) {
- return null;
- }
-}
\ No newline at end of file
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/api/SoyDataCodec.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/api/SoyDataCodec.java
deleted file mode 100644
index 6267419205..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/api/SoyDataCodec.java
+++ /dev/null
@@ -1,43 +0,0 @@
-package com.pmease.gitop.web.common.soy.api;
-
-import com.google.template.soy.data.SoyData;
-
-/**
- * Encode/decode the data to/from the soy data. The concrete implementations should be thread-safe.
- * The concrete implementation should be registered to guice.
- *
- */
-public interface SoyDataCodec<T> {
-
- /**
- * Encode an object to soy data
- *
- * @param object
- * @return encoded soy data
- */
- SoyData encode(T object);
-
- /**
- * Decode the soy data to an object
- *
- * @param data
- * @return decoded object
- */
- T decode(SoyData data);
-
- /**
- * Returns whether this codec can encode the type or not
- *
- * @param type
- * @return
- */
- boolean canEncode(Class<?> type);
-
- /**
- * Returns whether this codec can decode the type or not
- *
- * @param type
- * @return
- */
- boolean canDecode(Class<? extends SoyData> type);
-}
\ No newline at end of file
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/api/SoyDataMapper.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/api/SoyDataMapper.java
deleted file mode 100644
index 6d3deea626..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/api/SoyDataMapper.java
+++ /dev/null
@@ -1,18 +0,0 @@
-package com.pmease.gitop.web.common.soy.api;
-
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
-
-/**
- * Marks a class as requiring a custom mapper to convert it to SoyData.
- *
- * @since 1.1
- */
-@Retention(RetentionPolicy.RUNTIME)
-@Target(ElementType.TYPE)
-public @interface SoyDataMapper {
- @SuppressWarnings("rawtypes")
- Class<? extends SoyDataCodec> codec();
-}
\ No newline at end of file
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/api/SoyException.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/api/SoyException.java
deleted file mode 100644
index 843cb148f4..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/api/SoyException.java
+++ /dev/null
@@ -1,17 +0,0 @@
-package com.pmease.gitop.web.common.soy.api;
-
-public class SoyException extends RuntimeException {
- private static final long serialVersionUID = 1L;
-
- public SoyException(String message) {
- super(message);
- }
-
- public SoyException(String message, Throwable e) {
- super(message, e);
- }
-
- public SoyException(Throwable e) {
- super(e);
- }
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/api/SoyTemplateRenderer.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/api/SoyTemplateRenderer.java
deleted file mode 100644
index c7c8d3d91c..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/api/SoyTemplateRenderer.java
+++ /dev/null
@@ -1,29 +0,0 @@
-package com.pmease.gitop.web.common.soy.api;
-
-import java.util.Map;
-
-/**
- * Render google closure soy template on server side
- */
-public interface SoyTemplateRenderer {
-
- /**
- * Render a single template with the supplied data and injected data to an appendable
- *
- * @param key
- * @param out
- * @param data
- * @param ijData
- */
- void render(TemplateKey key, Appendable out, Map<String, ?> data, Map<String, ?> ijData);
-
- /**
- * Render a single template with the supplied data to an appendable
- *
- * @param key
- * @param out
- * @param data
- */
- void render(TemplateKey key, Appendable out, Map<String, ?> data);
-
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/api/TemplateKey.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/api/TemplateKey.java
deleted file mode 100644
index 501b7e018e..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/api/TemplateKey.java
+++ /dev/null
@@ -1,49 +0,0 @@
-package com.pmease.gitop.web.common.soy.api;
-
-import java.io.Serializable;
-
-import com.google.common.base.Objects;
-
-public class TemplateKey implements Serializable {
- private static final long serialVersionUID = 1L;
-
- private final String namespace;
- private final String templateName;
-
- TemplateKey(final String namespace, final String templateName) {
- this.namespace = namespace;
- this.templateName = templateName;
- }
-
- public static TemplateKey of(String namespace, String templateName) {
- return new TemplateKey(namespace, templateName);
- }
-
- public String getNamespace() {
- return namespace;
- }
-
- public String getTemplateName() {
- return templateName;
- }
-
- @Override
- public boolean equals(Object other) {
- if (!(other instanceof TemplateKey)) return false;
- if (this == other) return true;
-
- TemplateKey rhs = (TemplateKey) other;
- return Objects.equal(this.namespace, rhs.namespace)
- && Objects.equal(this.templateName, rhs.templateName);
- }
-
- @Override
- public int hashCode() {
- return Objects.hashCode(namespace, templateName);
- }
-
- @Override
- public String toString() {
- return namespace + "." + templateName;
- }
-}
\ No newline at end of file
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/ClasspathFileSetSupplier.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/ClasspathFileSetSupplier.java
deleted file mode 100644
index 83761bb5fb..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/ClasspathFileSetSupplier.java
+++ /dev/null
@@ -1,53 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl;
-
-import java.net.URL;
-import java.util.Set;
-import java.util.regex.Pattern;
-
-import javax.inject.Inject;
-import javax.inject.Named;
-import javax.inject.Singleton;
-
-import org.reflections.Reflections;
-import org.reflections.scanners.ResourcesScanner;
-import org.reflections.util.ClasspathHelper;
-import org.reflections.util.ConfigurationBuilder;
-
-import com.google.common.collect.Sets;
-import com.google.common.io.Resources;
-
-@Singleton
-class ClasspathFileSetSupplier implements SoyFileSetSupplier {
-
- public static final String CFG_SOY_PACKAGES = "CFG_SOY_PACKAGES";
- private static final String SOY = ".*\\.soy";
- private static final Pattern PATTERN_SOY = Pattern.compile(SOY);
-
-// private final Set<String> packageNames;
-
- private final Set<String> soyPackages;
-
- @Inject
- ClasspathFileSetSupplier(@Named(CFG_SOY_PACKAGES) Set<String> soyPackages) {
- this.soyPackages = soyPackages;
- }
-
- @Override
- public Set<URL> get() {
- ConfigurationBuilder builder = new ConfigurationBuilder();
- for (String each : soyPackages) {
- builder.addUrls(ClasspathHelper.forPackage(each));
- }
-
- builder.setScanners(new ResourcesScanner());
- Reflections reflections = new Reflections(builder);
- Set<URL> files = Sets.newHashSet();
- Set<String> set = reflections.getResources(PATTERN_SOY);
-
- for (String each : set) {
- files.add(Resources.getResource(each));
- }
-
- return files;
- }
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/DefaultSoyDataConverter.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/DefaultSoyDataConverter.java
deleted file mode 100644
index 106955f151..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/DefaultSoyDataConverter.java
+++ /dev/null
@@ -1,32 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl;
-
-import javax.inject.Inject;
-import javax.inject.Singleton;
-
-import com.google.template.soy.data.SoyData;
-import com.pmease.gitop.web.common.soy.api.SoyDataCodec;
-import com.pmease.gitop.web.common.soy.impl.codec.SoyDataCodecFactory;
-
-@Singleton
-class DefaultSoyDataConverter implements SoyDataConverter {
-
- private final SoyDataCodecFactory codecs;
-
- @Inject
- DefaultSoyDataConverter(SoyDataCodecFactory codecs) {
- this.codecs = codecs;
- }
-
- @SuppressWarnings({"rawtypes", "unchecked"})
- @Override
- public SoyData toSoyData(Object obj) {
- SoyDataCodec codec = codecs.getCodec(obj);
- return codec.encode(obj);
- }
-
- @Override
- public Object fromSoyData(SoyData data) {
- SoyDataCodec<?> codec = codecs.getCodec(data);
- return codec.decode(data);
- }
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/SoyDataConverter.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/SoyDataConverter.java
deleted file mode 100644
index 27282b6d76..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/SoyDataConverter.java
+++ /dev/null
@@ -1,11 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl;
-
-import com.google.template.soy.data.SoyData;
-
-public interface SoyDataConverter {
-
- SoyData toSoyData(Object obj);
-
- Object fromSoyData(SoyData data);
-
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/SoyFileSetSupplier.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/SoyFileSetSupplier.java
deleted file mode 100644
index 3553093933..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/SoyFileSetSupplier.java
+++ /dev/null
@@ -1,10 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl;
-
-import java.net.URL;
-import java.util.Set;
-
-public interface SoyFileSetSupplier {
-
- Set<URL> get();
-
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/SoyManager.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/SoyManager.java
deleted file mode 100644
index 7c85895ae0..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/SoyManager.java
+++ /dev/null
@@ -1,189 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl;
-
-import java.net.URL;
-import java.util.Collections;
-import java.util.Map;
-import java.util.Set;
-
-import javax.annotation.Nullable;
-import javax.inject.Inject;
-import javax.inject.Singleton;
-
-import com.google.common.cache.CacheBuilder;
-import com.google.common.cache.CacheLoader;
-import com.google.common.cache.LoadingCache;
-import com.google.common.collect.ImmutableSortedSet;
-import com.google.inject.Injector;
-import com.google.inject.Stage;
-import com.google.template.soy.SoyFileSet;
-import com.google.template.soy.data.SoyMapData;
-import com.google.template.soy.msgs.SoyMsgBundle;
-import com.google.template.soy.parseinfo.SoyTemplateInfo;
-import com.google.template.soy.shared.SoyCssRenamingMap;
-import com.google.template.soy.tofu.SoyTofu;
-import com.pmease.gitop.web.common.soy.api.SoyTemplateRenderer;
-import com.pmease.gitop.web.common.soy.api.TemplateKey;
-import com.pmease.gitop.web.util.UrlUtils;
-
-@Singleton
-public class SoyManager implements SoyTemplateRenderer {
- private static final SoyTofu DIDNOTCOMPILE = new NullTofu();
-
- static class TofuInfo {
- final SoyTofu tofu;
- final long lastModified;
-
- TofuInfo(SoyTofu tofu, long lastModified) {
- this.tofu = tofu;
- this.lastModified = lastModified;
- }
- }
-
- private final LoadingCache<String, TofuInfo> tofuCache;
-
- private final Injector injector;
- private final Stage stage;
- private final SoyFileSetSupplier filesets;
- private final SoyDataConverter converter;
- private static final String DEFAULT_TOFU = SoyManager.class.getName() + ".tofu";
-
- @Inject
- SoyManager(final Injector injector,
- final Stage stage,
- final SoyFileSetSupplier filesets,
- final SoyDataConverter converter) {
- this.injector = injector;
- this.stage = stage;
- this.filesets = filesets;
- this.converter = converter;
-
- tofuCache = CacheBuilder.newBuilder().build(new CacheLoader<String, TofuInfo>() {
-
- @Override
- public TofuInfo load(String key) throws Exception {
- SoyFileSet.Builder builder = getSoyFileSetBuilder();
- Set<URL> urls = filesets.get();
- long lastModified = UrlUtils.getLastModified(urls);
- for (URL each : urls) {
- builder.add(each);
- }
-
- SoyTofu tofu = builder.build().compileToTofu();
- if (tofu != null) {
- return new TofuInfo(tofu, lastModified);
- } else {
- return new TofuInfo(DIDNOTCOMPILE, -1);
- }
- }
- });
- }
-
- private SoyFileSet.Builder getSoyFileSetBuilder() {
- return injector.getInstance(SoyFileSet.Builder.class);
- }
-
- private SoyTofu.Renderer getRenderer(TemplateKey key) {
- if (isDev()) {
- updateTofuCache(DEFAULT_TOFU);
- }
-
- SoyTofu tofu = tofuCache.getUnchecked(DEFAULT_TOFU).tofu;
- if (tofu == null) {
- tofu = DIDNOTCOMPILE;
- }
-
- return tofu.newRenderer(key.toString());
- }
-
- private void updateTofuCache(String key) {
- Set<URL> urls = filesets.get();
- long lastModified = UrlUtils.getLastModified(urls);
- TofuInfo info = tofuCache.getUnchecked(key);
- if (info.lastModified < lastModified || lastModified <= 0) {
- tofuCache.invalidate(key);
- }
- }
-
- private boolean isDev() {
- return stage == Stage.DEVELOPMENT;
- }
-
- @Override
- public void render(TemplateKey key, Appendable out, Map<String, ?> data, Map<String, ?> ijData) {
- SoyTofu.Renderer renderer = getRenderer(key);
- renderer.setData((SoyMapData) converter.toSoyData(data))
- .setIjData((SoyMapData) converter.toSoyData(ijData))
- .render(out);
- }
-
- @Override
- public void render(TemplateKey key, Appendable out, Map<String, ?> data) {
- render(key, out, data, Collections.<String, Object>emptyMap());
- }
-
- /*
- * Placeholder SoyTofu implementation to go into ConcurrentMap that can't take null.
- */
- private static class NullTofu implements SoyTofu {
- @Override
- public String getNamespace() {
- return null;
- }
-
- @Override
- public SoyTofu forNamespace(String namespace) {
- return null;
- }
-
- @Override
- public String render(SoyTemplateInfo templateInfo, Map<String, ?> data, SoyMsgBundle msgBundle) {
- return null;
- }
-
- @Override
- public String render(SoyTemplateInfo templateInfo, SoyMapData data, SoyMsgBundle msgBundle) {
- return null;
- }
-
- @Override
- public String render(String templateName, Map<String, ?> data, SoyMsgBundle msgBundle) {
- return null;
- }
-
- @Override
- public String render(String templateName, SoyMapData data, SoyMsgBundle msgBundle) {
- return null;
- }
-
- @Override
- public boolean isCaching() {
- return false;
- }
-
- @Override
- public Renderer newRenderer(SoyTemplateInfo soyTemplateInfo) {
- return null;
- }
-
- @Override
- public Renderer newRenderer(String s) {
- return null;
- }
-
- @Override
- public void addToCache(@Nullable SoyMsgBundle msgBundle,
- @Nullable SoyCssRenamingMap cssRenamingMap) {
-
- }
-
- @Override
- public ImmutableSortedSet<String> getUsedIjParamsForTemplate(SoyTemplateInfo templateInfo) {
- return null;
- }
-
- @Override
- public ImmutableSortedSet<String> getUsedIjParamsForTemplate(String templateName) {
- return null;
- }
- }
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/SoyTemplateModule.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/SoyTemplateModule.java
deleted file mode 100644
index a808f55cfc..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/SoyTemplateModule.java
+++ /dev/null
@@ -1,42 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl;
-
-import java.util.Set;
-
-import javax.inject.Singleton;
-
-import com.google.common.collect.ImmutableSet;
-import com.google.inject.AbstractModule;
-import com.google.inject.Key;
-import com.google.inject.TypeLiteral;
-import com.google.inject.name.Names;
-import com.google.template.soy.SoyModule;
-import com.google.template.soy.xliffmsgplugin.XliffMsgPluginModule;
-import com.pmease.gitop.web.common.soy.api.SoyTemplateRenderer;
-import com.pmease.gitop.web.common.soy.impl.codec.SoyDataCodecModule;
-import com.pmease.gitop.web.common.soy.impl.functions.SoyFunctionsModule;
-
-public class SoyTemplateModule extends AbstractModule {
-
- private final Set<String> autoConfigPackages;
-
- public SoyTemplateModule(Set<String> autoConfigPackages) {
- this.autoConfigPackages = ImmutableSet.<String>copyOf(autoConfigPackages);
- }
-
- @Override
- protected void configure() {
- install(new SoyModule());
- install(new XliffMsgPluginModule());
-
- install(new SoyDataCodecModule());
- install(new SoyFunctionsModule());
-
- bind(Key.get(new TypeLiteral<Set<String>>() {}, Names.named("CFG_SOY_PACKAGES")))
- .toInstance(autoConfigPackages);
-
- bind(SoyFileSetSupplier.class).to(ClasspathFileSetSupplier.class);
- bind(SoyTemplateRenderer.class).to(SoyManager.class).in(Singleton.class);
- bind(SoyDataConverter.class).to(DefaultSoyDataConverter.class).in(Singleton.class);
- }
-
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/ArrayCodec.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/ArrayCodec.java
deleted file mode 100644
index 105f5618e6..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/ArrayCodec.java
+++ /dev/null
@@ -1,49 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl.codec;
-
-import java.util.List;
-
-import javax.inject.Inject;
-
-import com.google.common.collect.Lists;
-import com.google.inject.Injector;
-import com.google.template.soy.data.SoyData;
-import com.google.template.soy.data.SoyListData;
-import com.pmease.gitop.web.common.soy.api.SoyDataCodec;
-
-public class ArrayCodec implements SoyDataCodec<Object> {
-
- final Injector injector;
-
- @Inject
- ArrayCodec(Injector injector) {
- this.injector = injector;
- }
-
- @Override
- public SoyData encode(Object object) {
- List<Object> list = Lists.newArrayList((Object[]) object);
-
- return injector.getInstance(ListCodec.class).encode(list);
- }
-
- @SuppressWarnings("unchecked")
- @Override
- public Object decode(SoyData data) {
- if (data instanceof SoyListData) {
- List<Object> objects = (List<Object>) injector.getInstance(ListCodec.class).decode(data);
- return objects.toArray(new Object[objects.size()]);
- }
-
- throw new UnsupportedOperationException("from [" + data + "] to array");
- }
-
- @Override
- public boolean canEncode(Class<?> clazz) {
- return clazz.isArray();
- }
-
- @Override
- public boolean canDecode(Class<? extends SoyData> type) {
- return false;
- }
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/BasicTypeCodec.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/BasicTypeCodec.java
deleted file mode 100644
index d2dfeb6174..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/BasicTypeCodec.java
+++ /dev/null
@@ -1,26 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl.codec;
-
-import com.google.template.soy.data.SoyData;
-import com.pmease.gitop.web.common.soy.api.SoyDataCodec;
-import com.pmease.gitop.web.util.Classes;
-import com.pmease.gitop.web.util.Generics;
-
-abstract class BasicTypeCodec<T> implements SoyDataCodec<T> {
-
- final Class<T> clazz;
-
- @SuppressWarnings("unchecked")
- BasicTypeCodec() {
- clazz = (Class<T>) Generics.getTypeParameter(getClass());
- }
-
- @Override
- public SoyData encode(T object) {
- return SoyData.createFromExistingData(object);
- }
-
- @Override
- public boolean canEncode(Class<?> kls) {
- return Classes.isAssignableFrom(clazz, kls);
- }
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/BooleanCodec.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/BooleanCodec.java
deleted file mode 100644
index 9634cddaba..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/BooleanCodec.java
+++ /dev/null
@@ -1,24 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl.codec;
-
-import com.google.template.soy.data.SoyData;
-import com.google.template.soy.data.restricted.BooleanData;
-import com.pmease.gitop.web.util.Classes;
-
-class BooleanCodec extends BasicTypeCodec<Boolean> {
-
- @Override
- public Boolean decode(SoyData data) {
- return data.booleanValue();
- }
-
- @Override
- public boolean canDecode(Class<? extends SoyData> type) {
- return Classes.isAssignableFrom(BooleanData.class, type);
- }
-
-// @Override
-// public boolean canHandle(Class<?> clazz) {
-// return Classes.isAssignableFrom(Boolean.class, clazz);
-// }
-
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/DefaultSoyDataCodecFactory.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/DefaultSoyDataCodecFactory.java
deleted file mode 100644
index 1c4396bbca..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/DefaultSoyDataCodecFactory.java
+++ /dev/null
@@ -1,88 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl.codec;
-
-import java.util.Set;
-
-import javax.inject.Inject;
-import javax.inject.Singleton;
-
-import com.google.inject.Injector;
-import com.google.template.soy.data.SoyData;
-import com.google.template.soy.data.SoyListData;
-import com.google.template.soy.data.SoyMapData;
-import com.google.template.soy.data.restricted.BooleanData;
-import com.google.template.soy.data.restricted.FloatData;
-import com.google.template.soy.data.restricted.IntegerData;
-import com.google.template.soy.data.restricted.NullData;
-import com.google.template.soy.data.restricted.StringData;
-import com.pmease.gitop.web.common.soy.api.SoyDataCodec;
-import com.pmease.gitop.web.common.soy.api.SoyDataMapper;
-
-@Singleton
-@SuppressWarnings("rawtypes")
-class DefaultSoyDataCodecFactory implements SoyDataCodecFactory {
- final Set<SoyDataCodec> codecs;
- final Injector injector;
-
- @Inject
- DefaultSoyDataCodecFactory(Injector injector, Set<SoyDataCodec> codecs) {
- this.codecs = codecs;
- this.injector = injector;
- }
-
- @Override
- public SoyDataCodec<?> getCodec(Object obj) {
- if (obj == null) {
- return injector.getInstance(NullValueCodec.class);
- }
-
- return getCodec(obj.getClass());
- }
-
- @Override
- @SuppressWarnings("unchecked")
- public SoyDataCodec<?> getCodec(Class<?> clazz) {
- for (SoyDataCodec each : codecs) {
- if (each.canEncode(clazz)) {
- return each;
- }
- }
-
- if (clazz.isAnnotationPresent(SoyDataMapper.class)) {
- SoyDataMapper mapper = clazz.getAnnotation(SoyDataMapper.class);
- return injector.getInstance(mapper.codec());
- }
-
- return getLazyObjectCodec();
- }
-
- private SoyDataCodec getLazyObjectCodec() {
- return injector.getInstance(PojoCodec.class);
- }
-
- @Override
- public SoyDataCodec<?> getCodec(SoyData data) {
- if (data == null || data == NullData.INSTANCE) {
- return injector.getInstance(NullValueCodec.class);
- }
-
- if (data instanceof LazySoyMapData) {
- return getLazyObjectCodec();
- } else if (data instanceof SoyMapData) {
- return injector.getInstance(MapCodec.class);
- } else if (data instanceof SoyListData) {
- return injector.getInstance(ListCodec.class);
- } else if (data instanceof EnumData) {
- return injector.getInstance(EnumCodec.class);
- } else if (data instanceof StringData) {
- return injector.getInstance(StringCodec.class);
- } else if (data instanceof BooleanData) {
- return injector.getInstance(BooleanCodec.class);
- } else if (data instanceof IntegerData) {
- return injector.getInstance(IntegerCodec.class);
- } else if (data instanceof FloatData) {
- return injector.getInstance(DoubleCodec.class);
- } else {
- throw new UnsupportedOperationException("Unknown soy data [" + data + "]");
- }
- }
-}
\ No newline at end of file
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/DoubleCodec.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/DoubleCodec.java
deleted file mode 100644
index 4ecdc56a15..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/DoubleCodec.java
+++ /dev/null
@@ -1,18 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl.codec;
-
-import com.google.template.soy.data.SoyData;
-import com.google.template.soy.data.restricted.FloatData;
-import com.pmease.gitop.web.util.Classes;
-
-class DoubleCodec extends BasicTypeCodec<Double> {
-
- @Override
- public Double decode(SoyData data) {
- return data.floatValue();
- }
-
- @Override
- public boolean canDecode(Class<? extends SoyData> type) {
- return Classes.isAssignableFrom(FloatData.class, type);
- }
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/EnumCodec.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/EnumCodec.java
deleted file mode 100644
index 49868f526d..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/EnumCodec.java
+++ /dev/null
@@ -1,34 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl.codec;
-
-import com.google.template.soy.data.SoyData;
-import com.pmease.gitop.web.common.soy.api.SoyDataCodec;
-import com.pmease.gitop.web.util.Classes;
-
-@SuppressWarnings("rawtypes")
-class EnumCodec implements SoyDataCodec<Enum>{
-
- @Override
- public SoyData encode(Enum object) {
- return new EnumData(object);
- }
-
- @Override
- public Enum decode(SoyData data) {
- if (data instanceof EnumData) {
- return ((EnumData) data).getValue();
- }
-
- throw new UnsupportedOperationException("Unable to convert soydata [" + data + "] to enum");
- }
-
- @Override
- public boolean canEncode(Class<?> clazz) {
- return clazz.isEnum() || Enum.class.isAssignableFrom(clazz);
- }
-
- @Override
- public boolean canDecode(Class<? extends SoyData> type) {
- return Classes.isAssignableFrom(EnumData.class, type);
- }
-
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/EnumData.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/EnumData.java
deleted file mode 100644
index aaba1dc9d3..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/EnumData.java
+++ /dev/null
@@ -1,34 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl.codec;
-
-import com.google.template.soy.data.SoyData;
-import com.google.template.soy.data.restricted.StringData;
-
-class EnumData extends SoyData {
-
- private final Enum<?> value;
-
- public EnumData(Enum<?> value) {
- this.value = value;
- }
-
- public Enum<?> getValue() {
- return value;
- }
-
- @Override
- public String toString() {
- return value.name();
- }
-
- @Override
- public boolean toBoolean() {
- return true;
- }
-
- @Override
- public boolean equals(Object other) {
- return this == other
- || (other instanceof EnumData && ((EnumData) other).getValue() == value)
- || (other instanceof StringData && value.name().equals(((StringData) other).getValue()));
- }
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/FloatCodec.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/FloatCodec.java
deleted file mode 100644
index 0cfe8e7213..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/FloatCodec.java
+++ /dev/null
@@ -1,18 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl.codec;
-
-import com.google.template.soy.data.SoyData;
-import com.google.template.soy.data.restricted.FloatData;
-import com.pmease.gitop.web.util.Classes;
-
-class FloatCodec extends BasicTypeCodec<Float> {
-
- @Override
- public Float decode(SoyData data) {
- return new Float(data.floatValue());
- }
-
- @Override
- public boolean canDecode(Class<? extends SoyData> type) {
- return Classes.isAssignableFrom(FloatData.class, type);
- }
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/IntegerCodec.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/IntegerCodec.java
deleted file mode 100644
index 1fe922fa03..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/IntegerCodec.java
+++ /dev/null
@@ -1,18 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl.codec;
-
-import com.google.template.soy.data.SoyData;
-import com.google.template.soy.data.restricted.IntegerData;
-import com.pmease.gitop.web.util.Classes;
-
-public class IntegerCodec extends BasicTypeCodec<Integer> {
-
- @Override
- public Integer decode(SoyData data) {
- return data.integerValue();
- }
-
- @Override
- public boolean canDecode(Class<? extends SoyData> type) {
- return Classes.isAssignableFrom(IntegerData.class, type);
- }
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/LazyReference.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/LazyReference.java
deleted file mode 100644
index 0ce6de72ba..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/LazyReference.java
+++ /dev/null
@@ -1,305 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl.codec;
-
-import java.io.Serializable;
-import java.lang.ref.Reference;
-import java.lang.ref.WeakReference;
-import java.util.concurrent.CancellationException;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.locks.AbstractQueuedSynchronizer;
-
-/**
- * Lazily loaded reference that is not constructed until required. This class is used to maintain a
- * reference to an object that is expensive to create and must be constructed once and once only.
- * This reference behaves as though the <code>final</code> keyword has been used (you cannot reset
- * it once it has been constructed). Object creation is guaranteed to be thread-safe and the first
- * thread that calls {@link #get()} will be the one that creates it.
- * <p>
- * Usage: clients need to implement the {@link #create()} method to return the object this reference
- * will hold.
- * <p>
- * For instance:
- * <p>
- *
- * <pre>
- * final LazyReference&lt;MyObject&gt; ref = new LazyReference() {
- * protected MyObject create() throws Exception {
- * // Do expensive object construction here
- * return new MyObject();
- * }
- * };
- * </pre>
- *
- * Then call {@link #get()} to get a reference to the referenced object:
- *
- * <pre>
- * MyObject myLazyLoadedObject = ref.get()
- * </pre>
- *
- * NOTE: Interruption policy is that if you want to be cancellable while waiting for another thread
- * to create the value, instead of calling {@link #get()} call {@link #getInterruptibly()}. However,
- * If your {@link #create()} method is interrupted and throws an {@link InterruptedException}, it is
- * treated as an application exception and will be the causal exception inside the runtime
- * {@link InitializationException} that {@link #get()} or {@link #getInterruptibly()} throws and
- * your {@link #create()} will not be called again.
- * <p>
- * This class is NOT {@link Serializable}.
- * <p>
- * Implementation note. This class extends {@link WeakReference} as {@link Reference} does not have
- * a public constructor. WeakReference is preferable as it does not have any members and therefore
- * doesn't increase the memory footprint. As we never pass a referent through to the super-class and
- * override {@link #get()}, the garbage collection semantics of WeakReference are irrelevant. The
- * referenced object will not become eligible for GC unless the object holding the reference to this
- * object is collectible.
- *
- * @param T the type of the contained element.
- */
-public abstract class LazyReference<T> extends WeakReference<T>
- implements
- com.google.common.base.Supplier<T> {
-
- private final Sync sync = new Sync();
-
- public LazyReference() {
- super(null);
- }
-
- /**
- * The object factory method, guaranteed to be called once and only once.
- *
- * @return the object that {@link #get()} and {@link #getInterruptibly()} will return.
- * @throws Exception if anything goes wrong, rethrown as an InitializationException from
- * {@link #get()} and {@link #getInterruptibly()}
- */
- protected abstract T create() throws Exception;
-
- /**
- * Get the lazily loaded reference in a non-cancellable manner. If your <code>create()</code>
- * method throws an Exception calls to <code>get()</code> will throw an InitializationException
- * which wraps the previously thrown exception.
- *
- * @return the object that {@link #create()} created.
- * @throws InitializationException if the {@link #create()} method throws an exception. The
- * {@link InitializationException#getCause()} will contain the exception thrown by the
- * {@link #create()} method
- */
- @Override
- public final T get() {
- boolean interrupted = false;
- try {
- while (true) {
- try {
- return getInterruptibly();
- } catch (final InterruptedException ignore) {
- // ignore and try again
- interrupted = true;
- }
- }
- } finally {
- if (interrupted) {
- Thread.currentThread().interrupt();
- }
- }
- }
-
- /**
- * Get the lazily loaded reference in a cancellable manner. If your <code>create()</code> method
- * throws an Exception, calls to <code>get()</code> will throw a RuntimeException which wraps the
- * previously thrown exception.
- *
- * @return the object that {@link #create()} created.
- * @throws InitializationException if the {@link #create()} method throws an exception. The
- * {@link InitializationException#getCause()} will contain the exception thrown by the
- * {@link #create()} method
- * @throws InterruptedException If the calling thread is Interrupted while waiting for another
- * thread to create the value (if the creating thread is interrupted while blocking on
- * something, the {@link InterruptedException} will be thrown as the causal exception of
- * the {@link InitializationException} to everybody calling this method).
- */
- public final T getInterruptibly() throws InterruptedException {
- if (!sync.isDone()) {
- sync.run();
- }
-
- try {
- return sync.get();
- } catch (final ExecutionException e) {
- throw new InitializationException(e);
- }
- }
-
- /**
- * Has the {@link #create()} reference been initialized.
- *
- * @return true if the task is complete
- */
- public boolean isInitialized() {
- return sync.isDone();
- }
-
- /**
- * Cancel the initializing operation if it has not already run. Will try and interrupt if it is
- * currently running.
- */
- public void cancel() {
- sync.cancel(true);
- }
-
- /**
- * If the factory {@link LazyReference#create()} method threw an exception, this wraps it.
- */
- public static class InitializationException extends RuntimeException {
- private static final long serialVersionUID = 3638376010285456759L;
-
- InitializationException(final ExecutionException e) {
- super((e.getCause() != null) ? e.getCause() : e);
- }
- }
-
- /**
- * Synchronization control for LazyReference. Note that this must be a non-static inner class in
- * order to invoke the protected <tt>create</tt> method. Taken from FutureTask AQS implementation
- * and pruned to be as compact as possible.
- *
- * Uses AQS sync state to represent run status.
- */
- private final class Sync extends AbstractQueuedSynchronizer {
-
- /**
- * only here to shut up the compiler warnings, the outer class is NOT serializable
- */
- private static final long serialVersionUID = -1645412544240373524L;
-
- /** State value representing that task is running */
- private static final int RUNNING = 1;
- /** State value representing that task ran */
- private static final int RAN = 2;
- /** State value representing that task was cancelled */
- private static final int CANCELLED = 4;
-
- /** The result to return from get() */
- private T result;
- /** The exception to throw from get() */
- private Throwable exception;
-
- /**
- * The thread running task. When nulled after set/cancel, this indicates that the results are
- * accessible. Must be volatile, to ensure visibility upon completion.
- */
- private volatile Thread runner;
-
- private boolean ranOrCancelled(final int state) {
- return (state & (RAN | CANCELLED)) != 0;
- }
-
- /**
- * Implements AQS base acquire to succeed if ran or cancelled
- */
- @Override
- protected int tryAcquireShared(final int ignore) {
- return isDone() ? 1 : -1;
- }
-
- /**
- * Implements AQS base release to always signal after setting final done status by nulling
- * runner thread.
- */
- @Override
- protected boolean tryReleaseShared(final int ignore) {
- runner = null;
- return true;
- }
-
- boolean isDone() {
- return ranOrCancelled(getState()) && (runner == null);
- }
-
- T get() throws InterruptedException, ExecutionException {
- acquireSharedInterruptibly(0);
- if (getState() == CANCELLED) {
- throw new CancellationException();
- }
- if (exception != null) {
- throw new ExecutionException(exception);
- }
- return result;
- }
-
- void set(final T v) {
- for (;;) {
- final int s = getState();
- if (s == RAN) {
- return;
- }
- if (s == CANCELLED) {
- // aggressively release to set runner to null,
- // in case we are racing with a cancel request
- // that will try to interrupt runner
- releaseShared(0);
- return;
- }
- if (compareAndSetState(s, RAN)) {
- result = v;
- releaseShared(0);
- return;
- }
- }
- }
-
- void setException(final Throwable t) {
- for (;;) {
- final int s = getState();
- if (s == RAN) {
- return;
- }
- if (s == CANCELLED) {
- // aggressively release to set runner to null,
- // in case we are racing with a cancel request
- // that will try to interrupt runner
- releaseShared(0);
- return;
- }
- if (compareAndSetState(s, RAN)) {
- exception = t;
- result = null;
- releaseShared(0);
- return;
- }
- }
- }
-
- void cancel(final boolean mayInterruptIfRunning) {
- for (;;) {
- final int s = getState();
- if (ranOrCancelled(s)) {
- return;
- }
- if (compareAndSetState(s, CANCELLED)) {
- break;
- }
- }
- if (mayInterruptIfRunning) {
- final Thread r = runner;
- if (r != null) {
- r.interrupt();
- }
- }
- releaseShared(0);
- }
-
- void run() {
- if (!compareAndSetState(0, RUNNING)) {
- return;
- }
- try {
- runner = Thread.currentThread();
- if (getState() == RUNNING) {
- set(create());
- } else {
- releaseShared(0); // cancel
- }
- } catch (final Throwable ex) {
- setException(ex);
- }
- }
- }
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/LazySoyMapData.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/LazySoyMapData.java
deleted file mode 100644
index 69f8914bd1..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/LazySoyMapData.java
+++ /dev/null
@@ -1,66 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl.codec;
-
-import java.lang.reflect.InvocationTargetException;
-import java.util.concurrent.Callable;
-
-import org.apache.commons.beanutils.PropertyUtils;
-
-import com.google.common.cache.CacheBuilder;
-import com.google.common.cache.CacheLoader;
-import com.google.common.cache.LoadingCache;
-import com.google.template.soy.data.SoyData;
-import com.google.template.soy.data.SoyMapData;
-import com.google.template.soy.data.restricted.NullData;
-import com.pmease.gitop.web.common.soy.impl.SoyDataConverter;
-
-class LazySoyMapData extends SoyMapData {
- private final LoadingCache<String, SoyData> cache;
- private final LazyReference<Object> delegate;
-
- public LazySoyMapData(final Callable<?> objectSource, final SoyDataConverter soyDataConverter) {
- super();
- cache = CacheBuilder.newBuilder().build(new CacheLoader<String, SoyData>() {
- @Override
- public SoyData load(final String from) {
- Object value;
- try {
- value = "class".equals(from) ? null : PropertyUtils.getProperty(delegate.get(), from);
- } catch (IllegalAccessException e) {
- throw new RuntimeException(e);
- } catch (InvocationTargetException e) {
- throw new RuntimeException(e);
- } catch (NoSuchMethodException e) {
- value = null;
- }
- return soyDataConverter.toSoyData(value);
- }
- });
-
- this.delegate = new LazyReference<Object>() {
- @Override
- protected Object create() throws Exception {
- return objectSource.call();
- }
- };
- }
-
- public Object getDelegate() {
- return delegate.get();
- }
-
- @Override
- public SoyData getSingle(String key) {
- SoyData soyData = super.getSingle(key);
- if (soyData != null && soyData != NullData.INSTANCE) {
- return soyData;
- } else {
- return cache.getUnchecked(key);
- }
- }
-
- @Override
- public String toString() {
- return getDelegate().toString();
- }
-
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/ListCodec.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/ListCodec.java
deleted file mode 100644
index 1b341a222a..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/ListCodec.java
+++ /dev/null
@@ -1,57 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl.codec;
-
-import javax.inject.Inject;
-
-import com.google.common.base.Function;
-import com.google.common.collect.Lists;
-import com.google.template.soy.data.SoyData;
-import com.google.template.soy.data.SoyListData;
-import com.pmease.gitop.web.common.soy.api.SoyDataCodec;
-import com.pmease.gitop.web.util.Classes;
-
-@SuppressWarnings("rawtypes")
-public class ListCodec implements SoyDataCodec<Iterable> {
-
- final SoyDataCodecFactory codecs;
-
- @Inject
- ListCodec(SoyDataCodecFactory codecs) {
- this.codecs = codecs;
- }
-
- @SuppressWarnings("unchecked")
- @Override
- public SoyData encode(Iterable list) {
- SoyListData listData = new SoyListData();
- for (Object o : list) {
- SoyDataCodec codec = codecs.getCodec(o);
- listData.add(codec.encode(o));
- }
-
- return listData;
- }
-
- @Override
- public Iterable decode(SoyData data) {
- if (data instanceof SoyListData) {
- return Lists.transform(((SoyListData) data).asList(), new Function<SoyData, Object>() {
- @Override
- public Object apply(SoyData from) {
- return codecs.getCodec(from).decode(from);
- }
- });
- }
-
- throw new UnsupportedOperationException("from [" + data + "] to list");
- }
-
- @Override
- public boolean canEncode(Class<?> clazz) {
- return Classes.isAssignableFrom(Iterable.class, clazz);
- }
-
- @Override
- public boolean canDecode(Class<? extends SoyData> type) {
- return Classes.isAssignableFrom(SoyListData.class, type);
- }
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/LongCodec.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/LongCodec.java
deleted file mode 100644
index 05db788e63..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/LongCodec.java
+++ /dev/null
@@ -1,32 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl.codec;
-
-import com.google.template.soy.data.SoyData;
-import com.pmease.gitop.web.common.soy.api.SoyDataCodec;
-import com.pmease.gitop.web.util.Classes;
-
-class LongCodec implements SoyDataCodec<Long> {
-
- @Override
- public SoyData encode(Long object) {
- if (object >= Integer.MIN_VALUE && object <= Integer.MAX_VALUE) {
- return SoyData.createFromExistingData(object.intValue());
- } else {
- return SoyData.createFromExistingData(object.doubleValue());
- }
- }
-
- @Override
- public Long decode(SoyData data) {
- return ((Number) data.numberValue()).longValue();
- }
-
- @Override
- public boolean canEncode(Class<?> clazz) {
- return Classes.isAssignableFrom(Long.class, clazz);
- }
-
- @Override
- public boolean canDecode(Class<? extends SoyData> type) {
- return false;
- }
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/MapCodec.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/MapCodec.java
deleted file mode 100644
index 94c2f817be..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/MapCodec.java
+++ /dev/null
@@ -1,64 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl.codec;
-
-import java.util.Map;
-
-import javax.inject.Inject;
-
-import com.google.common.base.Function;
-import com.google.common.collect.Maps;
-import com.google.template.soy.data.SoyData;
-import com.google.template.soy.data.SoyMapData;
-import com.pmease.gitop.web.common.soy.api.SoyDataCodec;
-import com.pmease.gitop.web.util.Classes;
-
-@SuppressWarnings("rawtypes")
-class MapCodec implements SoyDataCodec<Map>{
-
- final SoyDataCodecFactory codecs;
-
- @Inject
- MapCodec(SoyDataCodecFactory codecs) {
- this.codecs = codecs;
- }
-
- @SuppressWarnings("unchecked")
- @Override
- public SoyData encode(Map map) {
- SoyMapData soyMapData = new SoyMapData();
- for (Object obj : map.entrySet()) {
- Map.Entry entry = (Map.Entry) obj;
- String key = entry.getKey().toString();
- Object value = entry.getValue();
- SoyDataCodec codec = codecs.getCodec(value);
- SoyData soyValue = codec.encode(value);
- soyMapData.put(key, soyValue);
- }
-
- return soyMapData;
- }
-
- @Override
- public Map decode(SoyData data) {
- if (data instanceof SoyMapData) {
- return Maps.transformValues(((SoyMapData) data).asMap(), new Function<SoyData, Object>() {
-
- @Override
- public Object apply(SoyData input) {
- return codecs.getCodec(input).decode(input);
- }
- });
- }
-
- throw new UnsupportedOperationException("from data [" + data + "] to map");
- }
-
- @Override
- public boolean canEncode(Class<?> clazz) {
- return Classes.isAssignableFrom(Map.class, clazz);
- }
-
- @Override
- public boolean canDecode(Class<? extends SoyData> type) {
- return Classes.isAssignableFrom(SoyMapData.class, type);
- }
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/NullValueCodec.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/NullValueCodec.java
deleted file mode 100644
index e013b8986f..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/NullValueCodec.java
+++ /dev/null
@@ -1,22 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl.codec;
-
-import com.google.template.soy.data.SoyData;
-
-class NullValueCodec extends BasicTypeCodec<Object> {
-
- @Override
- public Object decode(SoyData data) {
- return null;
- }
-
- @Override
- public boolean canEncode(Class<?> clazz) {
- return false;
- }
-
- @Override
- public boolean canDecode(Class<? extends SoyData> type) {
- return false;
- }
-
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/PojoCodec.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/PojoCodec.java
deleted file mode 100644
index 709e5cfd8c..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/PojoCodec.java
+++ /dev/null
@@ -1,55 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl.codec;
-
-import java.util.concurrent.Callable;
-
-import javax.inject.Inject;
-
-import com.google.common.util.concurrent.Callables;
-import com.google.template.soy.data.SoyData;
-import com.pmease.gitop.web.common.soy.api.SoyDataCodec;
-import com.pmease.gitop.web.common.soy.impl.SoyDataConverter;
-import com.pmease.gitop.web.util.Classes;
-
-public class PojoCodec implements SoyDataCodec<Object> {
-
- final SoyDataConverter convert;
-
- @Inject
- PojoCodec(SoyDataConverter convert) {
- this.convert = convert;
- }
-
- @Override
- public SoyData encode(Object object) {
- return new LazySoyMapData(makeCallable(object), convert);
- }
-
- private Callable<?> makeCallable(final Object value) {
- if (value instanceof Callable) {
- return (Callable<?>) value;
- }
-
- return Callables.returning(value);
- }
-
- @Override
- public Object decode(SoyData data) {
- if (data instanceof LazySoyMapData) {
- return ((LazySoyMapData) data).getDelegate();
- }
-
- throw new UnsupportedOperationException("from [" + data + "] to object");
- }
-
- @Override
- public boolean canEncode(Class<?> clazz) {
- // Always return false, we'll handle pojo case manually, see
- // {@class DefaultSoyDataCodecFactory}
- return false;
- }
-
- @Override
- public boolean canDecode(Class<? extends SoyData> type) {
- return Classes.isAssignableFrom(LazySoyMapData.class, type);
- }
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/SoyDataCodecFactory.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/SoyDataCodecFactory.java
deleted file mode 100644
index 88d745f95e..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/SoyDataCodecFactory.java
+++ /dev/null
@@ -1,10 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl.codec;
-
-import com.google.template.soy.data.SoyData;
-import com.pmease.gitop.web.common.soy.api.SoyDataCodec;
-
-public interface SoyDataCodecFactory {
- SoyDataCodec<?> getCodec(Object obj);
- SoyDataCodec<?> getCodec(Class<?> clazz);
- SoyDataCodec<?> getCodec(SoyData data);
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/SoyDataCodecModule.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/SoyDataCodecModule.java
deleted file mode 100644
index 0ce7d41a6c..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/SoyDataCodecModule.java
+++ /dev/null
@@ -1,34 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl.codec;
-
-import javax.inject.Singleton;
-
-import com.google.inject.AbstractModule;
-import com.google.inject.Binder;
-import com.google.inject.multibindings.Multibinder;
-import com.pmease.gitop.web.common.soy.api.SoyDataCodec;
-
-public class SoyDataCodecModule extends AbstractModule {
- @Override
- protected void configure() {
- bind(SoyDataCodecFactory.class).to(DefaultSoyDataCodecFactory.class);
-
- requireBinding(SoyDataCodecFactory.class);
- register(binder(), BooleanCodec.class);
- register(binder(), DoubleCodec.class);
- register(binder(), EnumCodec.class);
- register(binder(), FloatCodec.class);
- register(binder(), IntegerCodec.class);
- register(binder(), ListCodec.class);
- register(binder(), ArrayCodec.class);
- register(binder(), PojoCodec.class);
- register(binder(), LongCodec.class);
- register(binder(), MapCodec.class);
- register(binder(), NullValueCodec.class);
- register(binder(), StringCodec.class);
- }
-
- public static void register(Binder binder, Class<? extends SoyDataCodec<?>> codecClass) {
- Multibinder.newSetBinder(binder, SoyDataCodec.class)
- .addBinding().to(codecClass).in(Singleton.class);
- }
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/StringCodec.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/StringCodec.java
deleted file mode 100644
index df02c72e34..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/codec/StringCodec.java
+++ /dev/null
@@ -1,18 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl.codec;
-
-import com.google.template.soy.data.SoyData;
-import com.google.template.soy.data.restricted.StringData;
-import com.pmease.gitop.web.util.Classes;
-
-class StringCodec extends BasicTypeCodec<String> {
-
- @Override
- public String decode(SoyData data) {
- return data.stringValue();
- }
-
- @Override
- public boolean canDecode(Class<? extends SoyData> type) {
- return Classes.isAssignableFrom(StringData.class, type);
- }
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/functions/SoyFunctionsModule.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/functions/SoyFunctionsModule.java
deleted file mode 100644
index ec9dd67af3..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/functions/SoyFunctionsModule.java
+++ /dev/null
@@ -1,17 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl.functions;
-
-import com.google.inject.AbstractModule;
-import com.google.inject.multibindings.Multibinder;
-import com.google.template.soy.shared.restricted.SoyFunction;
-
-public class SoyFunctionsModule extends AbstractModule {
-
- @Override
- protected void configure() {
- Multibinder<SoyFunction> soyFunctionsSetBinder =
- Multibinder.newSetBinder(binder(), SoyFunction.class);
-
- soyFunctionsSetBinder.addBinding().to(ToLowercaseFunction.class);
- }
-
-}
\ No newline at end of file
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/functions/ToLowercaseFunction.java b/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/functions/ToLowercaseFunction.java
deleted file mode 100644
index ac6326b4cb..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/common/soy/impl/functions/ToLowercaseFunction.java
+++ /dev/null
@@ -1,50 +0,0 @@
-package com.pmease.gitop.web.common.soy.impl.functions;
-
-import static com.google.template.soy.shared.restricted.SoyJavaRuntimeFunctionUtils.toSoyData;
-
-import java.util.List;
-import java.util.Set;
-
-import com.google.common.collect.ImmutableSet;
-import com.google.inject.Singleton;
-import com.google.template.soy.data.SoyData;
-import com.google.template.soy.javasrc.restricted.JavaExpr;
-import com.google.template.soy.javasrc.restricted.SoyJavaSrcFunction;
-import com.google.template.soy.jssrc.restricted.JsExpr;
-import com.google.template.soy.jssrc.restricted.SoyJsSrcFunction;
-import com.google.template.soy.shared.restricted.SoyPureFunction;
-import com.google.template.soy.tofu.restricted.SoyAbstractTofuFunction;
-
-@Singleton
-@SoyPureFunction
-class ToLowercaseFunction extends SoyAbstractTofuFunction
- implements SoyJsSrcFunction, SoyJavaSrcFunction {
-
- @Override
- public String getName() {
- return "toLowercase";
- }
-
- @Override
- public Set<Integer> getValidArgsSizes() {
- return ImmutableSet.of(1);
- }
-
- @Override
- public SoyData compute(List<SoyData> args) {
- SoyData arg = args.get(0);
-
- String s = arg.stringValue();
- return toSoyData(s.toLowerCase());
- }
-
- @Override
- public JavaExpr computeForJavaSrc(List<JavaExpr> args) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public JsExpr computeForJsSrc(List<JsExpr> args) {
- throw new UnsupportedOperationException();
- }
-}
\ No newline at end of file
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/component/avatar/AvatarImage.java b/gitop.web/src/main/java/com/pmease/gitop/web/component/avatar/AvatarImage.java
index 64732c2c65..be7e71711d 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/component/avatar/AvatarImage.java
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/component/avatar/AvatarImage.java
@@ -9,10 +9,9 @@ import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
-import com.google.common.base.Strings;
-import com.pmease.gitop.model.Repository;
+import com.pmease.gitop.core.Gitop;
+import com.pmease.gitop.core.manager.ConfigManager;
import com.pmease.gitop.model.User;
-import com.pmease.gitop.web.GitopWebApp;
@StatelessComponent
public class AvatarImage extends Panel {
@@ -21,20 +20,9 @@ public class AvatarImage extends Panel {
private User user;
- public static enum AvatarImageType {
- USER, REPOSITORY
- }
-
- private final AvatarImageType imageType;
-
public AvatarImage(String id, User user) {
- this(id, user, AvatarImageType.USER);
- }
-
- public AvatarImage(String id, User user, AvatarImageType imageType) {
super(id);
this.user = user;
- this.imageType = imageType;
}
@Override
@@ -47,20 +35,12 @@ public class AvatarImage extends Panel {
}
private Component createAvatarImage() {
- if (imageType == AvatarImageType.USER) {
- if (GitopWebApp.get().isGravatarEnabled() && Strings.isNullOrEmpty(user.getAvatarUrl())) {
- return (new GravatarImage("avatar", Model.of(user.getEmail())));
- } else {
- PageParameters params = new PageParameters();
- params.set("type", AvatarImageType.USER.name().toLowerCase());
- params.set("id", user.getId());
- return (new StatelessAvatarImage("avatar", params));
- }
+ boolean gravatarEnabled = Gitop.getInstance(ConfigManager.class).getSystemSetting().isGravatarEnabled();
+ if (gravatarEnabled && !user.getLocalAvatar().exists()) {
+ return (new GravatarImage("avatar", Model.of(user.getEmail())));
} else {
- Repository repo = (Repository) getDefaultModelObject();
PageParameters params = new PageParameters();
- params.set("type", AvatarImageType.REPOSITORY.name().toLowerCase());
- params.set("id", repo.getId());
+ params.set("id", user.getId());
return (new StatelessAvatarImage("avatar", params));
}
}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/component/avatar/AvatarImageResource.java b/gitop.web/src/main/java/com/pmease/gitop/web/component/avatar/AvatarImageResource.java
index b630657c29..7a59047420 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/component/avatar/AvatarImageResource.java
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/component/avatar/AvatarImageResource.java
@@ -1,20 +1,18 @@
package com.pmease.gitop.web.component.avatar;
-import java.io.File;
import java.io.IOException;
+import org.apache.tika.mime.MediaType;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.request.resource.DynamicImageResource;
-import com.google.common.base.Strings;
-import com.google.common.base.Throwables;
import com.google.common.io.Files;
import com.pmease.commons.loader.AppLoader;
+import com.pmease.gitop.core.Gitop;
import com.pmease.gitop.core.manager.UserManager;
import com.pmease.gitop.model.User;
import com.pmease.gitop.web.GitopWebApp;
-import com.pmease.gitop.web.SitePaths;
-import com.pmease.gitop.web.component.avatar.AvatarImage.AvatarImageType;
+import com.pmease.gitop.web.service.FileTypes;
public class AvatarImageResource extends DynamicImageResource {
@@ -23,34 +21,21 @@ public class AvatarImageResource extends DynamicImageResource {
@Override
protected byte[] getImageData(Attributes attributes) {
PageParameters params = attributes.getParameters();
- AvatarImageType imageType = AvatarImageType.valueOf(params.get("type")
- .toString().toUpperCase());
long id = params.get("id").toLong();
- File avatarFile = null;
- if (imageType == AvatarImageType.USER) {
- User user = AppLoader.getInstance(UserManager.class).get(id);
+ User user = AppLoader.getInstance(UserManager.class).get(id);
- if (!Strings.isNullOrEmpty(user.getAvatarUrl())) {
- avatarFile = new File(SitePaths.get().userAvatarDir(id),
- user.getAvatarUrl());
- }
- } else {
- // Repository repository =
- // AppLoader.getInstance(RepositoryManager.class).get(id);
- // if (!Strings.isNullOrEmpty(repository.getAvatarUrl())) {
- // avatarFile = new File(GitopWebApp.getRepositoryAvatarDir(id),
- // repository.getAvatarUrl());
- // }
- }
-
- if (avatarFile != null && avatarFile.exists()) {
- setFormat("image/" + Files.getFileExtension(avatarFile.getName()));
+ if (user.getLocalAvatar().exists()) {
+ byte[] imageData;
try {
- return Files.toByteArray(avatarFile);
+ imageData = Files.toByteArray(user.getLocalAvatar());
} catch (IOException e) {
- throw Throwables.propagate(e);
+ throw new RuntimeException(e);
}
+ MediaType mediaType = Gitop.getInstance(FileTypes.class).getMediaType(
+ user.getLocalAvatar().getAbsolutePath(), imageData);
+ setFormat(mediaType.getType());
+ return imageData;
} else {
setFormat("image/jpg");
return GitopWebApp.get().getDefaultUserAvatar();
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/component/avatar/GravatarImage.java b/gitop.web/src/main/java/com/pmease/gitop/web/component/avatar/GravatarImage.java
index 8d97cfbf4a..5a5a7a12fc 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/component/avatar/GravatarImage.java
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/component/avatar/GravatarImage.java
@@ -4,31 +4,31 @@ import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.image.NonCachingImage;
import org.apache.wicket.model.IModel;
-import com.pmease.gitop.web.GitopWebApp;
import com.pmease.gitop.web.util.Gravatar;
public class GravatarImage extends NonCachingImage {
- private static final long serialVersionUID = 1L;
-
- public GravatarImage(String id, IModel<String> emailModel) {
- super(id, emailModel);
- }
-
- static final int GRAVAR_IMAGE_SIZE = 256;
-
- @Override
- protected void onComponentTag(ComponentTag tag) {
- String email = (String) getDefaultModelObject();
- if (email != null && GitopWebApp.get().isGravatarEnabled()) {
- tag.put("src", Gravatar.getURL(email, GRAVAR_IMAGE_SIZE));
- } else {
- tag.put("src", "assets/img/empty-avatar.jpg");
- }
- }
-
- @Override
- protected boolean getStatelessHint() {
- return true;
- }
+ private static final long serialVersionUID = 1L;
+
+ private static final int GRAVAR_IMAGE_SIZE = 256;
+
+ public GravatarImage(String id, IModel<String> emailModel) {
+ super(id, emailModel);
+ }
+
+ @Override
+ protected void onComponentTag(ComponentTag tag) {
+ String email = (String) getDefaultModelObject();
+ if (email != null) {
+ tag.put("src", Gravatar.getURL(email, GRAVAR_IMAGE_SIZE));
+ } else {
+ tag.put("src", "assets/img/empty-avatar.jpg");
+ }
+ }
+
+ @Override
+ protected boolean getStatelessHint() {
+ return true;
+ }
+
}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/component/choice/UserChoiceProvider.java b/gitop.web/src/main/java/com/pmease/gitop/web/component/choice/UserChoiceProvider.java
index acfa842c3e..a8deba5449 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/component/choice/UserChoiceProvider.java
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/component/choice/UserChoiceProvider.java
@@ -12,12 +12,11 @@ import org.hibernate.criterion.Restrictions;
import org.json.JSONException;
import org.json.JSONWriter;
-import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.pmease.gitop.core.Gitop;
+import com.pmease.gitop.core.manager.ConfigManager;
import com.pmease.gitop.core.manager.UserManager;
import com.pmease.gitop.model.User;
-import com.pmease.gitop.web.GitopWebApp;
import com.pmease.gitop.web.common.wicket.util.WicketUtils;
import com.pmease.gitop.web.component.avatar.AvatarImageResourceReference;
import com.pmease.gitop.web.util.Gravatar;
@@ -69,15 +68,13 @@ public class UserChoiceProvider extends ChoiceProvider<User> {
}
private String getAvatarUrl(User user) {
- String path = user.getAvatarUrl();
- if (GitopWebApp.get().isGravatarEnabled() && Strings.isNullOrEmpty(path)) {
+ boolean gravatarEnabled = Gitop.getInstance(ConfigManager.class).getSystemSetting().isGravatarEnabled();
+ if (gravatarEnabled && !user.getLocalAvatar().exists()) {
return Gravatar.getURL(user.getEmail());
} else {
CharSequence url = RequestCycle.get().urlFor(
new AvatarImageResourceReference(),
- WicketUtils.newPageParams(
- "id",String.valueOf(user.getId()),
- "type", "user"));
+ WicketUtils.newPageParams("id",String.valueOf(user.getId())));
return url.toString() + "?antiCache=" + System.currentTimeMillis();
}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/component/link/GitPersonLink.java b/gitop.web/src/main/java/com/pmease/gitop/web/component/link/GitPersonLink.java
deleted file mode 100644
index f9d2069151..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/component/link/GitPersonLink.java
+++ /dev/null
@@ -1,149 +0,0 @@
-package com.pmease.gitop.web.component.link;
-
-import org.apache.wicket.AttributeModifier;
-import org.apache.wicket.Component;
-import org.apache.wicket.markup.html.WebMarkupContainer;
-import org.apache.wicket.markup.html.basic.Label;
-import org.apache.wicket.markup.html.link.AbstractLink;
-import org.apache.wicket.markup.html.link.BookmarkablePageLink;
-import org.apache.wicket.markup.html.link.Link;
-import org.apache.wicket.markup.html.panel.Fragment;
-import org.apache.wicket.markup.html.panel.Panel;
-import org.apache.wicket.model.IModel;
-import org.apache.wicket.model.Model;
-
-import com.google.common.base.Optional;
-import com.google.common.base.Strings;
-import com.pmease.gitop.model.User;
-import com.pmease.gitop.web.component.avatar.AvatarImage;
-import com.pmease.gitop.web.component.avatar.GravatarImage;
-import com.pmease.gitop.web.page.PageSpec;
-import com.pmease.gitop.web.page.account.home.AccountHomePage;
-import com.pmease.gitop.web.page.repository.api.GitPerson;
-
-/**
- * Displays git person, name and avatar
- *
- */
-public class GitPersonLink extends Panel {
- private static final long serialVersionUID = 1L;
-
- public static enum Mode {
- NAME,
- AVATAR,
- NAME_AND_AVATAR
- }
-
- private final Mode mode;
- private WebMarkupContainer link;
- private Component image;
-
- private boolean enableTooltip;
- private String tooltipPosition;
-
- public GitPersonLink(String id, IModel<GitPerson> person, Mode mode) {
- super(id, person);
- this.mode = mode;
- }
-
- @Override
- protected void onInitialize() {
- super.onInitialize();
-
- GitPerson person = getPerson();
- Optional<User> user = person.asUser();
-
- link = newLink("link", user);
- add(link);
-
- if (mode == Mode.NAME_AND_AVATAR || mode == Mode.AVATAR) {
- if (user.isPresent()) {
- image = new AvatarImage("avatar", user.get());
- link.add(image);
- } else {
- Fragment frag = new Fragment("avatar", "imgfrag", this);
- image = new GravatarImage("img", Model.of(person.getEmailAddress()));
- frag.add(image);
- link.add(frag);
- }
- }
-
- if (mode == Mode.NAME_AND_AVATAR || mode == Mode.NAME) {
- if (user.isPresent()) {
- link.add(new Label("name", user.get().getName()));
- } else {
- link.add(new Label("name", person.getName()));
- }
- }
-
- if (mode == Mode.AVATAR) {
- link.add(new WebMarkupContainer("name").setVisibilityAllowed(false));
- }
-
- if (mode == Mode.NAME) {
- link.add(new WebMarkupContainer("avatar").setVisibilityAllowed(false));
- }
-
- setupTooltip(user.isPresent() ? user.get().getName() : person.getName());
- }
-
- public GitPersonLink enableTooltip() {
- return enableTooltip(null);
- }
-
- public GitPersonLink enableTooltip(String position) {
- this.enableTooltip = true;
- this.tooltipPosition = position;
- return this;
- }
-
- private void setupTooltip(String title) {
- if (!enableTooltip) {
- return;
- }
-
- Component c = null;
- if (image != null) {
- c = image;
- } else if (link != null) {
- c = link;
- } else {
- throw new IllegalStateException();
- }
-
- c.add(AttributeModifier.append("class", "has-tip"));
- if (!Strings.isNullOrEmpty(tooltipPosition)) {
- c.add(AttributeModifier.replace("data-placement", tooltipPosition));
- }
-
- c.add(AttributeModifier.replace("title", title));
- }
-
- @SuppressWarnings("serial")
- protected WebMarkupContainer newLink(String id, Optional<User> user) {
- if (user.isPresent()) {
- return new BookmarkablePageLink<Void>(id,
- AccountHomePage.class,
- PageSpec.forUser(user.get()));
- } else {
- AbstractLink link = new Link<Void>(id){
- @Override
- protected void onConfigure() {
- super.onConfigure();
-
- setEnabled(false);
- }
-
- @Override
- public void onClick() {
- }
- };
-
- return link;
- }
- }
-
- private GitPerson getPerson() {
- return (GitPerson) getDefaultModelObject();
- }
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/page/PageSpec.java b/gitop.web/src/main/java/com/pmease/gitop/web/page/PageSpec.java
index d88199356a..96fc12b0a1 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/page/PageSpec.java
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/page/PageSpec.java
@@ -13,7 +13,6 @@ import com.google.common.collect.Lists;
import com.pmease.gitop.model.Repository;
import com.pmease.gitop.model.User;
import com.pmease.gitop.web.common.wicket.util.WicketUtils;
-import com.pmease.gitop.web.component.avatar.AvatarImage.AvatarImageType;
import com.pmease.gitop.web.page.account.home.AccountHomePage;
import com.pmease.gitop.web.page.repository.source.RepositoryHomePage;
import com.pmease.gitop.web.util.UrlUtils;
@@ -28,8 +27,7 @@ public class PageSpec {
public static final String TAB = "tab";
public static PageParameters avatarOfUser(User user) {
- return WicketUtils.newPageParams(TYPE, AvatarImageType.USER.name()
- .toLowerCase(), ID, String.valueOf(user.getId()));
+ return WicketUtils.newPageParams(ID, String.valueOf(user.getId()));
}
public static PageParameters forUser(User user) {
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/page/account/setting/profile/AccountProfilePage.java b/gitop.web/src/main/java/com/pmease/gitop/web/page/account/setting/profile/AccountProfilePage.java
index 945ef0e711..81e5535b88 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/page/account/setting/profile/AccountProfilePage.java
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/page/account/setting/profile/AccountProfilePage.java
@@ -1,6 +1,5 @@
package com.pmease.gitop.web.page.account.setting.profile;
-import java.io.File;
import java.io.IOException;
import org.apache.wicket.ajax.AjaxRequestTarget;
@@ -17,14 +16,11 @@ import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.lang.Bytes;
import com.google.common.base.Preconditions;
-import com.google.common.base.Strings;
import com.google.common.base.Throwables;
-import com.google.common.io.Files;
import com.pmease.commons.loader.AppLoader;
import com.pmease.commons.util.FileUtils;
import com.pmease.gitop.core.manager.UserManager;
import com.pmease.gitop.model.User;
-import com.pmease.gitop.web.SitePaths;
import com.pmease.gitop.web.common.wicket.component.vex.AjaxConfirmButton;
import com.pmease.gitop.web.common.wicket.form.BaseForm;
import com.pmease.gitop.web.common.wicket.form.textfield.TextFieldElement;
@@ -138,45 +134,12 @@ public class AccountProfilePage extends AccountSettingPage {
User user = getUser();
if (upload != null) {
- File dir = SitePaths.get().userAvatarDir(user);
- if (!dir.exists()) {
- try {
- FileUtils.forceMkdir(dir);
- } catch (IOException e) {
- throw Throwables.propagate(e);
- }
- }
-
- String filename = upload.getClientFileName();
- String ext = Files.getFileExtension(filename);
- File avatarFile;
-
- // delete old avatar file
- String avatarPath = user.getAvatarUrl();
- if (!Strings.isNullOrEmpty(avatarPath)
- && !(avatarPath.startsWith("http") || avatarPath
- .startsWith("https"))) {
- avatarFile = new File(dir, user.getAvatarUrl());
- // TODO: lock avatarFile for write
- if (avatarFile.exists()) {
- try {
- FileUtils.forceDelete(avatarFile);
- } catch (IOException e) {
- throw Throwables.propagate(e);
- }
- }
- }
-
- avatarFile = new File(dir, "avatar." + ext);
try {
- upload.writeTo(avatarFile);
+ upload.writeTo(user.getLocalAvatar());
} catch (IOException e) {
throw Throwables.propagate(e);
}
- user.setAvatarUrl(avatarFile.getName());
- AppLoader.getInstance(UserManager.class).save(user);
-
send(getPage(), Broadcast.BREADTH, new AvatarChanged(target));
form.success("Your avatar has been updated successfully");
target.add(form);
@@ -190,8 +153,7 @@ public class AccountProfilePage extends AccountSettingPage {
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
User user = getUser();
- user.setAvatarUrl(null);
- AppLoader.getInstance(UserManager.class).save(user);
+ FileUtils.deleteFile(user.getLocalAvatar());
send(getPage(), Broadcast.BREADTH, new AvatarChanged(target));
form.success("Your avatar has been reset to the default.");
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/resource/ResourceLocator.java b/gitop.web/src/main/java/com/pmease/gitop/web/resource/ResourceLocator.java
new file mode 100644
index 0000000000..535ae28b75
--- /dev/null
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/resource/ResourceLocator.java
@@ -0,0 +1,5 @@
+package com.pmease.gitop.web.resource;
+
+public class ResourceLocator {
+
+}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/resource/TestResource.java b/gitop.web/src/main/java/com/pmease/gitop/web/resource/TestResource.java
deleted file mode 100644
index 2693588290..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/resource/TestResource.java
+++ /dev/null
@@ -1,168 +0,0 @@
-package com.pmease.gitop.web.resource;
-
-import io.dropwizard.jackson.Jackson;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Iterator;
-import java.util.List;
-
-import javax.inject.Inject;
-import javax.servlet.http.HttpServletRequest;
-import javax.ws.rs.Consumes;
-import javax.ws.rs.GET;
-import javax.ws.rs.POST;
-import javax.ws.rs.Path;
-import javax.ws.rs.core.Context;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
-
-import org.apache.commons.fileupload.FileItem;
-import org.apache.commons.fileupload.FileItemFactory;
-import org.apache.commons.fileupload.FileUploadException;
-import org.apache.commons.fileupload.disk.DiskFileItemFactory;
-import org.apache.commons.fileupload.servlet.ServletFileUpload;
-import org.apache.commons.io.IOUtils;
-import org.apache.shiro.authc.credential.PasswordService;
-
-import com.fasterxml.jackson.annotation.JsonProperty;
-import com.fasterxml.jackson.core.JsonParseException;
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.core.type.TypeReference;
-import com.fasterxml.jackson.databind.JsonMappingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.google.common.base.Optional;
-import com.google.common.collect.Lists;
-import com.pmease.gitop.core.Gitop;
-import com.pmease.gitop.core.manager.UserManager;
-import com.pmease.gitop.model.User;
-import com.pmease.gitop.web.SitePaths;
-import com.pmease.gitop.web.util.StandardObjectMapper;
-
-@Path("/test")
-public class TestResource {
-
- ObjectMapper objectMapper = StandardObjectMapper.getInstance();
- @Inject UserManager userManager;
-
- @GET
- @Path("/users")
- public String populateUsers() {
- InputStream in = TestResource.class.getResourceAsStream("users.json");
- int count = 0;
- try {
- List<User> users = objectMapper.readValue(in, new TypeReference<List<User>>(){});
- count = users.size();
- String passwordHash = Gitop.getInstance(PasswordService.class).encryptPassword("12345");
-
- for (User each : users) {
- each.setId(null);
- each.setPasswordHash(passwordHash);
- userManager.save(each);
- }
- } catch (JsonParseException e) {
- e.printStackTrace();
- } catch (JsonMappingException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- IOUtils.closeQuietly(in);
- }
-
- return count + " users saved";
- }
-
- @GET
- @Path("/files")
- public String getResult() throws JsonProcessingException {
- Result result = new Result();
- UploadFile file = new UploadFile();
- file.name = "abc.png";
- file.size = 1234L;
- result.files.add(file);
-
- file = new UploadFile();
- file.name = "def.png";
- file.size = 1234L;
- result.files.add(file);
-
- return objectMapper.writeValueAsString(Optional.<Result>of(result));
- }
-
- @POST
- @Path("/upload")
- @Consumes(MediaType.MULTIPART_FORM_DATA)
- public Response uploadFile(@Context HttpServletRequest request) throws JsonProcessingException {
- String candidateName = null;
- File uploadDir = SitePaths.get().uploadsDir();
-
- Result result = new Result();
-
- // checks whether there is a file upload request or not
- if (ServletFileUpload.isMultipartContent(request)) {
- final FileItemFactory factory = new DiskFileItemFactory();
- final ServletFileUpload fileUpload = new ServletFileUpload(factory);
-
- try {
- /*
- * parseRequest returns a list of FileItem but in old
- * (pre-java5) style
- */
- final List<FileItem> items = fileUpload.parseRequest(request);
-
- if (items != null) {
- final Iterator<FileItem> iter = items.iterator();
- while (iter.hasNext()) {
- final FileItem item = iter.next();
- final String itemName = item.getName();
- final String fieldName = item.getFieldName();
- final String fieldValue = item.getString();
-
- if (item.isFormField()) {
- candidateName = fieldValue;
- System.out.println("Field Name: " + fieldName
- + ", Field Value: " + fieldValue);
- System.out.println("Candidate Name: "
- + candidateName);
- } else {
- final File savedFile = new File(uploadDir, itemName);
- System.out.println("Saving the file: "
- + savedFile.getName());
- item.write(savedFile);
-
- UploadFile f = new UploadFile();
- f.name = savedFile.getName();
- f.size = savedFile.length();
- result.files.add(f);
- }
-
- }
- }
- } catch (FileUploadException fue) {
- fue.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- ObjectMapper mapper = Jackson.newObjectMapper();
- String str = mapper.writeValueAsString(result);
-
- System.out.println("Returned Response Status: " + str);
- return Response.ok().entity(str).build();
- }
-
- static class Result {
- @JsonProperty
- List<UploadFile> files = Lists.newArrayList();
- }
-
- static class UploadFile {
- @JsonProperty
- String name;
- @JsonProperty
- long size;
- }
-}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/util/Gravatar.java b/gitop.web/src/main/java/com/pmease/gitop/web/util/Gravatar.java
index 8ed124d888..cfd0b36ffc 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/util/Gravatar.java
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/util/Gravatar.java
@@ -14,384 +14,432 @@ import java.security.NoSuchAlgorithmException;
*/
public class Gravatar {
- public static final String PRE = "http://www.gravatar.com/avatar/";
- public static final String SSL_PRE = "https://secure.gravatar.com/avatar/";
-
- // default images
- public static final int DFLT_DEFAULT = 2;
- public static final int DFLT_FOUR_O_FOUR = 0;
- public static final int DFLT_MYSTERY_MAN = 1;
- public static final int DFLT_IDENTICON = 2;
- public static final int DFLT_MONSTERID = 3;
- public static final int DFLT_WAVATAR = 4;
- public static final int DFLT_RETRO = 5;
- public static final int DFLT_BLANK = 6;
- private static final String[] dfltImages = {"404", "mm", "identicon", "monsterid", "wavatar",
- "retro", "blank"};
-
- // ratings
- public static final int RTNG_NONE = -1;
- public static final int RTNG_G = 0;
- public static final int RTNG_PG = 1;
- public static final int RTNG_R = 2;
- public static final int RTNG_X = 3;
- private static final String[] ratings = {"g", "pg", "r", "x"};
-
- private String email;
- private String hash;
- private int size;
- private int dflt = DFLT_DEFAULT;
- private boolean forceDefault;
- private String customDefault;
- private int rating;
-
- /**
- * Create a new Gravatar object for a particular email address.
- *
- * @param email the email for this Gravatar
- */
- public Gravatar(String email) {
- this.email = email;
- byte[] buf = email.trim().toLowerCase().getBytes();
- try {
- this.hash = Gravatar.toHexString(MessageDigest.getInstance("MD5").digest(buf));
- this.hash = this.hash.toLowerCase();
- } catch (NoSuchAlgorithmException e) {
- // all java implementations must implement MD5
- }
- }
-
- /**
- * Get the email associated with this Gravatar.
- *
- * @return the email associated with this Gravatar
- */
- public String getEmail() {
- return this.email;
- }
-
- /**
- * Set the image size of this Gravatar in pixels.
- *
- * @param size size of image in pixels [1-512], 0 for default size
- */
- public void setSize(int size) {
- if (size >= 0 && size < 512) {
- this.size = size;
- }
- }
-
- /**
- * Get the size set for this Gravatar.
- *
- * @return the size set for this Gravatar [1-512], 0 for default size
- */
- public int getSize() {
- return this.size;
- }
-
- /**
- * Set the image returned if there is no Gravatar for the email.
- *
- * @param dflt must be one of the DFLT_XXX constants
- */
- public void setDefault(int dflt) {
- if (dflt >= DFLT_DEFAULT && dflt <= DFLT_BLANK) {
- // blank out the custom if one was set
- this.customDefault = null;
- this.dflt = dflt;
- }
- }
-
- /**
- * Get the default image setting of this Gravatar.
- *
- * @return the default image setting of this Gravatar
- */
- public int getDefault() {
- return this.dflt;
- }
-
- /**
- * Sets whether the default Gravatar should always be returned.
- *
- * @param forceDefault true if the default Gravatar should always be returned
- */
- public void setForceDefault(boolean forceDefault) {
- this.forceDefault = forceDefault;
- }
-
- /**
- * Get whether this Gravatar is set to always return the default image.
- *
- * @return true if this Gravatar is set to always return the default image
- */
- public boolean getForceDefault() {
- return this.forceDefault;
- }
-
- /**
- * Set the URL of a custom default image to use.
- *
- * @param url the URL of the custom default image to use
- */
- public void setCustomDefault(String url) {
- try {
- if (url != null) {
- // UTF-8 should always be supported
- this.customDefault = URLEncoder.encode(url, "UTF-8");
- this.dflt = DFLT_DEFAULT;
- }
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- }
-
- /**
- * Get the URL of the custom default image set for this Gravatar.
- *
- * @return the URL of the custom default image set, null if not set
- */
- public String getCustomDefault() {
- try {
- // UTF-8 should always be supported
- return this.customDefault == null ? null : URLDecoder.decode(this.customDefault, "UTF-8");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- return null;
- }
-
- /**
- * Set the maximum allowed rating of the image to return.
- *
- * @param rating must be one of the RTNG_XXX constants
- */
- public void setRating(int rating) {
- if (rating >= RTNG_NONE && rating <= RTNG_X) {
- this.rating = rating;
- }
- }
-
- /**
- * Get the maximum allowed rating of image set for this Gravatar.
- *
- * @return the rating set for this Gravatar
- */
- public int getRating() {
- return this.rating;
- }
-
- /**
- * Get the URL for this Gravatar.
- *
- * @return URL for this Gravatar
- */
- public String getURL() {
- return this.getURL(false);
- }
-
- /**
- * Get the URL for this Gravatar, HTTPS if specified.
- *
- * @param secure true if HTTPS is desired
- * @return URL for this Gravatar
- */
- public String getURL(boolean secure) {
- StringBuilder str = new StringBuilder();
- str.append(secure ? SSL_PRE : PRE);
- str.append(this.hash);
- str.append(this.getOptions());
- return str.toString();
- }
-
- /**
- * Build the options string to append to the URL.
- *
- * @return the options portion of the URL
- */
- private String getOptions() {
- StringBuilder opts = new StringBuilder();
- opts.append("?");
- // size
- if (this.size != 0) {
- opts.append("s=");
- opts.append(this.size);
- }
- // default
- if (this.dflt >= 0) {
- // check if we just added an option
- opts.append(opts.length() > 1 ? "&" : "");
- opts.append("d=");
- opts.append(dfltImages[this.dflt]);
- }
- // custom default
- if (this.customDefault != null) {
- // check if we just added an option
- opts.append(opts.length() > 1 ? "&" : "");
- opts.append("d=");
- opts.append(this.customDefault);
- }
- // force default
- if (this.forceDefault) {
- // check if we just added an option
- opts.append(opts.length() > 1 ? "&" : "");
- opts.append("f=y");
- }
- // rating
- if (this.rating != RTNG_NONE) {
- // check if we just added an option
- opts.append(opts.length() > 1 ? "&" : "");
- opts.append("r=");
- opts.append(ratings[this.rating]);
- }
- return (opts.length() > 1) ? opts.toString() : "";
- }
-
- // ////////////////////
- // Static Interface //
- // ////////////////////
-
- /**
- * Get the Gravatar URL for a particular email.
- *
- * @param email the email for this Gravatar
- * @return the URL of the Gravatar
- */
- public static String getURL(String email) {
- return Gravatar.getURL(email, false);
- }
-
- /**
- * Get the optionally HTTPS Gravatar URL for a particular email.
- *
- * @param email the email for this Gravatar
- * @param secure true if HTTPS is desired
- * @return the URL of the Gravatar
- */
- public static String getURL(String email, boolean secure) {
- return Gravatar.getURL(email, 0, DFLT_DEFAULT, false, RTNG_NONE, secure);
- }
-
- /**
- * Get the Gravatar URL for an email and with a specified size
- *
- * @param email the email for this Gravatar
- * @param size the desired size for this Gravatar [1-512]
- * @return the URL of the Gravatar
- */
- public static String getURL(String email, int size) {
- return Gravatar.getURL(email, size, DFLT_DEFAULT);
- }
-
- /**
- * Get the Gravatar for an email with a specified size and default image.
- *
- * @param email the email for this Gravatar
- * @param size the desired size for this Gravatar [1-512]
- * @param dflt must be one of the DFLT_XXX constants
- * @return the URL of the Gravatar
- */
- public static String getURL(String email, int size, int dflt) {
- return Gravatar.getURL(email, size, dflt, RTNG_NONE);
- }
-
- /**
- * Get the Gravatar for an email with a specified size, default image, and maximum rating.
- *
- * @param email the email for this Gravatar
- * @param size the desired size for this Gravatar [1-512]
- * @param dflt must be one of the DFLT_XXX constants
- * @param rating must be one of the RTNG_XXX constants
- * @return the URL of the Gravatar
- */
- public static String getURL(String email, int size, int dflt, int rating) {
- return Gravatar.getURL(email, size, dflt, false, rating);
- }
-
- /**
- * Get the Gravatar for an email with a specified size, default image, maximum rating, and
- * specified force default option.
- *
- * @param email the email for this Gravatar
- * @param size the desired size for this Gravatar [1-512]
- * @param dflt must be one of the DFLT_XXX constants
- * @param forceDefault true if the default should always be returned
- * @param rating must be one of the RTNG_XXX constants
- * @return the URL of the Gravatar
- */
- public static String getURL(String email, int size, int dflt, boolean forceDefault, int rating) {
- return Gravatar.getURL(email, size, dflt, forceDefault, rating, false);
- }
-
- /**
- * Get the Gravatar for an email with a specified size, custom default, maximum rating, and
- * specified force default option.
- *
- * @param email the email for this Gravatar
- * @param size the desired size for this Gravatar [1-512]
- * @param customDefault URL of custom default image
- * @param forceDefault true if the default should always be returned
- * @param rating must be one of the RTNG_XXX constants
- * @return the URL of the Gravatar
- */
- public static String getURL(String email, int size, String customDefault, boolean forceDefault,
- int rating) {
- return Gravatar.getURL(email, size, customDefault, forceDefault, rating, false);
- }
-
- /**
- * Get the Gravatar for an email with a specified size, custom default, maximum rating, and
- * specified force default option, and HTTPS preference.
- *
- * @param email the email for this Gravatar
- * @param size the desired size for this Gravatar [1-512]
- * @param customDefault URL of custom default image
- * @param forceDefault true if the default should always be returned
- * @param rating must be one of the RTNG_XXX constants
- * @param secure true if HTTPS desired
- * @return the URL of the Gravatar
- */
- public static String getURL(String email, int size, int dflt, boolean forceDefault, int rating,
- boolean secure) {
- Gravatar g = new Gravatar(email);
- g.setSize(size);
- g.setDefault(dflt);
- g.setForceDefault(forceDefault);
- g.setRating(rating);
- return g.getURL(secure);
- }
-
- /**
- * Get the Gravatar for an email with a specified size, custom default, maximum rating, specified
- * force default option, and HTTPS preference.
- *
- * @param email the email for this Gravatar
- * @param size the desired size for this Gravatar [1-512]
- * @param customDefault URL of custom default image
- * @param forceDefault true if the default should always be returned
- * @param rating must be one of the RTNG_XXX constants
- * @param secure true if HTTPS desired
- * @return the URL of the Gravatar
- */
- public static String getURL(String email, int size, String customDefault, boolean forceDefault,
- int rating, boolean secure) {
- Gravatar g = new Gravatar(email);
- g.setSize(size);
- g.setCustomDefault(customDefault);
- g.setForceDefault(forceDefault);
- g.setRating(rating);
- return g.getURL(secure);
- }
-
- /**
- * Convert an array of bytes to a Hex String
- *
- * @param buf the bytes to convert
- * @return the Hex String of the bytes
- */
- private static String toHexString(byte[] buf) {
- BigInteger bi = new BigInteger(1, buf);
- return String.format("%0" + (buf.length << 1) + "X", bi);
- }
+ public static final String PRE = "http://www.gravatar.com/avatar/";
+ public static final String SSL_PRE = "https://secure.gravatar.com/avatar/";
+
+ // default images
+ public static final int DFLT_DEFAULT = 2;
+ public static final int DFLT_FOUR_O_FOUR = 0;
+ public static final int DFLT_MYSTERY_MAN = 1;
+ public static final int DFLT_IDENTICON = 2;
+ public static final int DFLT_MONSTERID = 3;
+ public static final int DFLT_WAVATAR = 4;
+ public static final int DFLT_RETRO = 5;
+ public static final int DFLT_BLANK = 6;
+ private static final String[] dfltImages = { "404", "mm", "identicon",
+ "monsterid", "wavatar", "retro", "blank" };
+
+ // ratings
+ public static final int RTNG_NONE = -1;
+ public static final int RTNG_G = 0;
+ public static final int RTNG_PG = 1;
+ public static final int RTNG_R = 2;
+ public static final int RTNG_X = 3;
+ private static final String[] ratings = { "g", "pg", "r", "x" };
+
+ private String email;
+ private String hash;
+ private int size;
+ private int dflt = DFLT_DEFAULT;
+ private boolean forceDefault;
+ private String customDefault;
+ private int rating;
+
+ /**
+ * Create a new Gravatar object for a particular email address.
+ *
+ * @param email
+ * the email for this Gravatar
+ */
+ public Gravatar(String email) {
+ this.email = email;
+ byte[] buf = email.trim().toLowerCase().getBytes();
+ try {
+ this.hash = Gravatar.toHexString(MessageDigest.getInstance("MD5")
+ .digest(buf));
+ this.hash = this.hash.toLowerCase();
+ } catch (NoSuchAlgorithmException e) {
+ // all java implementations must implement MD5
+ }
+ }
+
+ /**
+ * Get the email associated with this Gravatar.
+ *
+ * @return the email associated with this Gravatar
+ */
+ public String getEmail() {
+ return this.email;
+ }
+
+ /**
+ * Set the image size of this Gravatar in pixels.
+ *
+ * @param size
+ * size of image in pixels [1-512], 0 for default size
+ */
+ public void setSize(int size) {
+ if (size >= 0 && size < 512) {
+ this.size = size;
+ }
+ }
+
+ /**
+ * Get the size set for this Gravatar.
+ *
+ * @return the size set for this Gravatar [1-512], 0 for default size
+ */
+ public int getSize() {
+ return this.size;
+ }
+
+ /**
+ * Set the image returned if there is no Gravatar for the email.
+ *
+ * @param dflt
+ * must be one of the DFLT_XXX constants
+ */
+ public void setDefault(int dflt) {
+ if (dflt >= DFLT_DEFAULT && dflt <= DFLT_BLANK) {
+ // blank out the custom if one was set
+ this.customDefault = null;
+ this.dflt = dflt;
+ }
+ }
+
+ /**
+ * Get the default image setting of this Gravatar.
+ *
+ * @return the default image setting of this Gravatar
+ */
+ public int getDefault() {
+ return this.dflt;
+ }
+
+ /**
+ * Sets whether the default Gravatar should always be returned.
+ *
+ * @param forceDefault
+ * true if the default Gravatar should always be returned
+ */
+ public void setForceDefault(boolean forceDefault) {
+ this.forceDefault = forceDefault;
+ }
+
+ /**
+ * Get whether this Gravatar is set to always return the default image.
+ *
+ * @return true if this Gravatar is set to always return the default image
+ */
+ public boolean getForceDefault() {
+ return this.forceDefault;
+ }
+
+ /**
+ * Set the URL of a custom default image to use.
+ *
+ * @param url
+ * the URL of the custom default image to use
+ */
+ public void setCustomDefault(String url) {
+ try {
+ if (url != null) {
+ // UTF-8 should always be supported
+ this.customDefault = URLEncoder.encode(url, "UTF-8");
+ this.dflt = DFLT_DEFAULT;
+ }
+ } catch (UnsupportedEncodingException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Get the URL of the custom default image set for this Gravatar.
+ *
+ * @return the URL of the custom default image set, null if not set
+ */
+ public String getCustomDefault() {
+ try {
+ // UTF-8 should always be supported
+ return this.customDefault == null ? null : URLDecoder.decode(
+ this.customDefault, "UTF-8");
+ } catch (UnsupportedEncodingException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ /**
+ * Set the maximum allowed rating of the image to return.
+ *
+ * @param rating
+ * must be one of the RTNG_XXX constants
+ */
+ public void setRating(int rating) {
+ if (rating >= RTNG_NONE && rating <= RTNG_X) {
+ this.rating = rating;
+ }
+ }
+
+ /**
+ * Get the maximum allowed rating of image set for this Gravatar.
+ *
+ * @return the rating set for this Gravatar
+ */
+ public int getRating() {
+ return this.rating;
+ }
+
+ /**
+ * Get the URL for this Gravatar.
+ *
+ * @return URL for this Gravatar
+ */
+ public String getURL() {
+ return this.getURL(false);
+ }
+
+ /**
+ * Get the URL for this Gravatar, HTTPS if specified.
+ *
+ * @param secure
+ * true if HTTPS is desired
+ * @return URL for this Gravatar
+ */
+ public String getURL(boolean secure) {
+ StringBuilder str = new StringBuilder();
+ str.append(secure ? SSL_PRE : PRE);
+ str.append(this.hash);
+ str.append(this.getOptions());
+ return str.toString();
+ }
+
+ /**
+ * Build the options string to append to the URL.
+ *
+ * @return the options portion of the URL
+ */
+ private String getOptions() {
+ StringBuilder opts = new StringBuilder();
+ opts.append("?");
+ // size
+ if (this.size != 0) {
+ opts.append("s=");
+ opts.append(this.size);
+ }
+ // default
+ if (this.dflt >= 0) {
+ // check if we just added an option
+ opts.append(opts.length() > 1 ? "&" : "");
+ opts.append("d=");
+ opts.append(dfltImages[this.dflt]);
+ }
+ // custom default
+ if (this.customDefault != null) {
+ // check if we just added an option
+ opts.append(opts.length() > 1 ? "&" : "");
+ opts.append("d=");
+ opts.append(this.customDefault);
+ }
+ // force default
+ if (this.forceDefault) {
+ // check if we just added an option
+ opts.append(opts.length() > 1 ? "&" : "");
+ opts.append("f=y");
+ }
+ // rating
+ if (this.rating != RTNG_NONE) {
+ // check if we just added an option
+ opts.append(opts.length() > 1 ? "&" : "");
+ opts.append("r=");
+ opts.append(ratings[this.rating]);
+ }
+ return (opts.length() > 1) ? opts.toString() : "";
+ }
+
+ // ////////////////////
+ // Static Interface //
+ // ////////////////////
+
+ /**
+ * Get the Gravatar URL for a particular email.
+ *
+ * @param email
+ * the email for this Gravatar
+ * @return the URL of the Gravatar
+ */
+ public static String getURL(String email) {
+ return Gravatar.getURL(email, false);
+ }
+
+ /**
+ * Get the optionally HTTPS Gravatar URL for a particular email.
+ *
+ * @param email
+ * the email for this Gravatar
+ * @param secure
+ * true if HTTPS is desired
+ * @return the URL of the Gravatar
+ */
+ public static String getURL(String email, boolean secure) {
+ return Gravatar
+ .getURL(email, 0, DFLT_DEFAULT, false, RTNG_NONE, secure);
+ }
+
+ /**
+ * Get the Gravatar URL for an email and with a specified size
+ *
+ * @param email
+ * the email for this Gravatar
+ * @param size
+ * the desired size for this Gravatar [1-512]
+ * @return the URL of the Gravatar
+ */
+ public static String getURL(String email, int size) {
+ return Gravatar.getURL(email, size, DFLT_DEFAULT);
+ }
+
+ /**
+ * Get the Gravatar for an email with a specified size and default image.
+ *
+ * @param email
+ * the email for this Gravatar
+ * @param size
+ * the desired size for this Gravatar [1-512]
+ * @param dflt
+ * must be one of the DFLT_XXX constants
+ * @return the URL of the Gravatar
+ */
+ public static String getURL(String email, int size, int dflt) {
+ return Gravatar.getURL(email, size, dflt, RTNG_NONE);
+ }
+
+ /**
+ * Get the Gravatar for an email with a specified size, default image, and
+ * maximum rating.
+ *
+ * @param email
+ * the email for this Gravatar
+ * @param size
+ * the desired size for this Gravatar [1-512]
+ * @param dflt
+ * must be one of the DFLT_XXX constants
+ * @param rating
+ * must be one of the RTNG_XXX constants
+ * @return the URL of the Gravatar
+ */
+ public static String getURL(String email, int size, int dflt, int rating) {
+ return Gravatar.getURL(email, size, dflt, false, rating);
+ }
+
+ /**
+ * Get the Gravatar for an email with a specified size, default image,
+ * maximum rating, and specified force default option.
+ *
+ * @param email
+ * the email for this Gravatar
+ * @param size
+ * the desired size for this Gravatar [1-512]
+ * @param dflt
+ * must be one of the DFLT_XXX constants
+ * @param forceDefault
+ * true if the default should always be returned
+ * @param rating
+ * must be one of the RTNG_XXX constants
+ * @return the URL of the Gravatar
+ */
+ public static String getURL(String email, int size, int dflt,
+ boolean forceDefault, int rating) {
+ return Gravatar.getURL(email, size, dflt, forceDefault, rating, false);
+ }
+
+ /**
+ * Get the Gravatar for an email with a specified size, custom default,
+ * maximum rating, and specified force default option.
+ *
+ * @param email
+ * the email for this Gravatar
+ * @param size
+ * the desired size for this Gravatar [1-512]
+ * @param customDefault
+ * URL of custom default image
+ * @param forceDefault
+ * true if the default should always be returned
+ * @param rating
+ * must be one of the RTNG_XXX constants
+ * @return the URL of the Gravatar
+ */
+ public static String getURL(String email, int size, String customDefault,
+ boolean forceDefault, int rating) {
+ return Gravatar.getURL(email, size, customDefault, forceDefault,
+ rating, false);
+ }
+
+ /**
+ * Get the Gravatar for an email with a specified size, custom default,
+ * maximum rating, and specified force default option, and HTTPS preference.
+ *
+ * @param email
+ * the email for this Gravatar
+ * @param size
+ * the desired size for this Gravatar [1-512]
+ * @param customDefault
+ * URL of custom default image
+ * @param forceDefault
+ * true if the default should always be returned
+ * @param rating
+ * must be one of the RTNG_XXX constants
+ * @param secure
+ * true if HTTPS desired
+ * @return the URL of the Gravatar
+ */
+ public static String getURL(String email, int size, int dflt,
+ boolean forceDefault, int rating, boolean secure) {
+ Gravatar g = new Gravatar(email);
+ g.setSize(size);
+ g.setDefault(dflt);
+ g.setForceDefault(forceDefault);
+ g.setRating(rating);
+ return g.getURL(secure);
+ }
+
+ /**
+ * Get the Gravatar for an email with a specified size, custom default,
+ * maximum rating, specified force default option, and HTTPS preference.
+ *
+ * @param email
+ * the email for this Gravatar
+ * @param size
+ * the desired size for this Gravatar [1-512]
+ * @param customDefault
+ * URL of custom default image
+ * @param forceDefault
+ * true if the default should always be returned
+ * @param rating
+ * must be one of the RTNG_XXX constants
+ * @param secure
+ * true if HTTPS desired
+ * @return the URL of the Gravatar
+ */
+ public static String getURL(String email, int size, String customDefault,
+ boolean forceDefault, int rating, boolean secure) {
+ Gravatar g = new Gravatar(email);
+ g.setSize(size);
+ g.setCustomDefault(customDefault);
+ g.setForceDefault(forceDefault);
+ g.setRating(rating);
+ return g.getURL(secure);
+ }
+
+ /**
+ * Convert an array of bytes to a Hex String
+ *
+ * @param buf
+ * the bytes to convert
+ * @return the Hex String of the bytes
+ */
+ private static String toHexString(byte[] buf) {
+ BigInteger bi = new BigInteger(1, buf);
+ return String.format("%0" + (buf.length << 1) + "X", bi);
+ }
}