2013-03-20 20:33:42 +01:00
|
|
|
package com.commafeed.frontend;
|
|
|
|
|
|
2013-03-27 20:56:45 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
import java.util.jar.JarFile;
|
|
|
|
|
|
2013-03-20 20:33:42 +01:00
|
|
|
import javax.enterprise.inject.spi.BeanManager;
|
|
|
|
|
import javax.naming.InitialContext;
|
|
|
|
|
import javax.naming.NamingException;
|
|
|
|
|
|
2013-03-23 19:46:09 +01:00
|
|
|
import org.apache.wicket.Application;
|
2013-03-30 19:06:32 +01:00
|
|
|
import org.apache.wicket.Component;
|
2013-03-20 20:33:42 +01:00
|
|
|
import org.apache.wicket.Page;
|
|
|
|
|
import org.apache.wicket.Session;
|
|
|
|
|
import org.apache.wicket.ajax.AjaxRequestTarget;
|
2013-03-30 19:06:32 +01:00
|
|
|
import org.apache.wicket.authorization.Action;
|
|
|
|
|
import org.apache.wicket.authorization.IAuthorizationStrategy;
|
2013-03-20 20:33:42 +01:00
|
|
|
import org.apache.wicket.authroles.authentication.AbstractAuthenticatedWebSession;
|
|
|
|
|
import org.apache.wicket.authroles.authentication.AuthenticatedWebApplication;
|
|
|
|
|
import org.apache.wicket.authroles.authorization.strategies.role.Roles;
|
|
|
|
|
import org.apache.wicket.cdi.CdiConfiguration;
|
|
|
|
|
import org.apache.wicket.cdi.ConversationPropagation;
|
|
|
|
|
import org.apache.wicket.core.request.handler.PageProvider;
|
|
|
|
|
import org.apache.wicket.core.request.handler.RenderPageRequestHandler;
|
|
|
|
|
import org.apache.wicket.core.request.handler.RenderPageRequestHandler.RedirectPolicy;
|
|
|
|
|
import org.apache.wicket.markup.html.WebPage;
|
|
|
|
|
import org.apache.wicket.request.IRequestHandler;
|
|
|
|
|
import org.apache.wicket.request.Request;
|
|
|
|
|
import org.apache.wicket.request.Response;
|
2013-03-30 19:06:32 +01:00
|
|
|
import org.apache.wicket.request.component.IRequestableComponent;
|
2013-03-20 20:33:42 +01:00
|
|
|
import org.apache.wicket.request.cycle.AbstractRequestCycleListener;
|
|
|
|
|
import org.apache.wicket.request.cycle.RequestCycle;
|
2013-03-27 20:56:45 +01:00
|
|
|
import org.jboss.vfs.VirtualFile;
|
|
|
|
|
import org.reflections.ReflectionsException;
|
|
|
|
|
import org.reflections.vfs.SystemDir;
|
|
|
|
|
import org.reflections.vfs.Vfs;
|
|
|
|
|
import org.reflections.vfs.ZipDir;
|
2013-03-20 20:33:42 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
2013-04-03 15:53:57 +02:00
|
|
|
import com.commafeed.frontend.pages.FaviconPage;
|
2013-04-05 16:31:42 +02:00
|
|
|
import com.commafeed.frontend.pages.GoogleImportCallbackPage;
|
|
|
|
|
import com.commafeed.frontend.pages.GoogleImportRedirectPage;
|
2013-03-23 16:17:19 +01:00
|
|
|
import com.commafeed.frontend.pages.HomePage;
|
|
|
|
|
import com.commafeed.frontend.pages.LogoutPage;
|
2013-04-09 11:10:26 +02:00
|
|
|
import com.commafeed.frontend.pages.TestRssPage;
|
|
|
|
|
import com.commafeed.frontend.pages.WelcomePage;
|
2013-03-20 20:33:42 +01:00
|
|
|
import com.commafeed.frontend.utils.exception.DisplayExceptionPage;
|
|
|
|
|
|
2013-03-27 20:56:45 +01:00
|
|
|
import de.agilecoders.wicket.Bootstrap;
|
|
|
|
|
import de.agilecoders.wicket.settings.BootstrapSettings;
|
|
|
|
|
|
2013-03-20 20:33:42 +01:00
|
|
|
public class CommaFeedApplication extends AuthenticatedWebApplication {
|
|
|
|
|
|
|
|
|
|
private Logger log = LoggerFactory.getLogger(CommaFeedApplication.class);
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void init() {
|
|
|
|
|
super.init();
|
|
|
|
|
|
2013-04-06 21:38:18 +02:00
|
|
|
mountPage("welcome", WelcomePage.class);
|
2013-03-21 18:40:09 +01:00
|
|
|
mountPage("logout", LogoutPage.class);
|
|
|
|
|
mountPage("error", DisplayExceptionPage.class);
|
2013-04-03 15:53:57 +02:00
|
|
|
mountPage("favicon", FaviconPage.class);
|
2013-04-05 16:31:42 +02:00
|
|
|
mountPage("google/import/redirect", GoogleImportRedirectPage.class);
|
|
|
|
|
mountPage("google/import/callback", GoogleImportCallbackPage.class);
|
2013-03-22 20:51:22 +01:00
|
|
|
|
2013-04-09 11:10:26 +02:00
|
|
|
mountPage("testfeed", TestRssPage.class);
|
|
|
|
|
|
2013-03-20 20:33:42 +01:00
|
|
|
setupInjection();
|
|
|
|
|
|
|
|
|
|
getMarkupSettings().setStripWicketTags(true);
|
|
|
|
|
getMarkupSettings().setCompressWhitespace(true);
|
|
|
|
|
getMarkupSettings().setDefaultMarkupEncoding("UTF-8");
|
|
|
|
|
|
|
|
|
|
getSecuritySettings().setAuthorizationStrategy(
|
2013-03-30 19:06:32 +01:00
|
|
|
new IAuthorizationStrategy() {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public <T extends IRequestableComponent> boolean isInstantiationAuthorized(
|
|
|
|
|
Class<T> componentClass) {
|
|
|
|
|
boolean authorized = true;
|
|
|
|
|
|
|
|
|
|
boolean restricted = componentClass
|
|
|
|
|
.isAnnotationPresent(SecurityCheck.class);
|
|
|
|
|
if (restricted) {
|
|
|
|
|
SecurityCheck annotation = componentClass
|
|
|
|
|
.getAnnotation(SecurityCheck.class);
|
|
|
|
|
Roles roles = CommaFeedSession.get().getRoles();
|
|
|
|
|
authorized = roles.hasAnyRole(new Roles(annotation
|
|
|
|
|
.value().name()));
|
|
|
|
|
}
|
|
|
|
|
return authorized;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean isActionAuthorized(Component component,
|
|
|
|
|
Action action) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-03-20 20:33:42 +01:00
|
|
|
|
|
|
|
|
getRequestCycleListeners().add(new AbstractRequestCycleListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public IRequestHandler onException(RequestCycle cycle, Exception ex) {
|
|
|
|
|
AjaxRequestTarget target = cycle.find(AjaxRequestTarget.class);
|
|
|
|
|
// redirect to the error page if ajax request, render error on
|
|
|
|
|
// current page otherwise
|
|
|
|
|
RedirectPolicy policy = target == null ? RedirectPolicy.NEVER_REDIRECT
|
|
|
|
|
: RedirectPolicy.AUTO_REDIRECT;
|
|
|
|
|
return new RenderPageRequestHandler(new PageProvider(
|
|
|
|
|
new DisplayExceptionPage(ex)), policy);
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-03-27 20:56:45 +01:00
|
|
|
Bootstrap.install(Application.get(), new BootstrapSettings());
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Class<? extends Page> getHomePage() {
|
2013-03-21 16:22:58 +01:00
|
|
|
return HomePage.class;
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void setupInjection() {
|
|
|
|
|
try {
|
|
|
|
|
BeanManager beanManager = (BeanManager) new InitialContext()
|
|
|
|
|
.lookup("java:comp/BeanManager");
|
|
|
|
|
new CdiConfiguration(beanManager).setPropagation(
|
|
|
|
|
ConversationPropagation.NONE).configure(this);
|
|
|
|
|
} catch (NamingException e) {
|
|
|
|
|
log.warn("Could not locate bean manager. CDI is disabled.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Session newSession(Request request, Response response) {
|
|
|
|
|
return new CommaFeedSession(request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected Class<? extends WebPage> getSignInPageClass() {
|
2013-04-06 21:38:18 +02:00
|
|
|
return WelcomePage.class;
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected Class<? extends AbstractAuthenticatedWebSession> getWebSessionClass() {
|
|
|
|
|
return CommaFeedSession.class;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-23 19:46:09 +01:00
|
|
|
public static CommaFeedApplication get() {
|
|
|
|
|
|
|
|
|
|
return (CommaFeedApplication) Application.get();
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-27 20:56:45 +01:00
|
|
|
/**
|
|
|
|
|
* Reflections fix for JbossAS7
|
|
|
|
|
*
|
|
|
|
|
* https://code.google.com/p/reflections/wiki/JBossIntegration
|
|
|
|
|
*/
|
|
|
|
|
static {
|
|
|
|
|
|
|
|
|
|
Vfs.addDefaultURLTypes(new Vfs.UrlType() {
|
|
|
|
|
public boolean matches(URL url) {
|
|
|
|
|
return url.getProtocol().equals("vfs");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Vfs.Dir createDir(URL url) {
|
|
|
|
|
VirtualFile content;
|
|
|
|
|
try {
|
|
|
|
|
content = (VirtualFile) url.openConnection().getContent();
|
|
|
|
|
} catch (Throwable e) {
|
|
|
|
|
throw new ReflectionsException(
|
|
|
|
|
"could not open url connection as VirtualFile ["
|
|
|
|
|
+ url + "]", e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vfs.Dir dir = null;
|
|
|
|
|
try {
|
|
|
|
|
dir = createDir(new java.io.File(content.getPhysicalFile()
|
|
|
|
|
.getParentFile(), content.getName()));
|
|
|
|
|
} catch (IOException e) { /* continue */
|
|
|
|
|
}
|
|
|
|
|
if (dir == null) {
|
|
|
|
|
try {
|
|
|
|
|
dir = createDir(content.getPhysicalFile());
|
|
|
|
|
} catch (IOException e) { /* continue */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return dir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vfs.Dir createDir(java.io.File file) {
|
|
|
|
|
try {
|
|
|
|
|
return file.exists() && file.canRead() ? file.isDirectory() ? new SystemDir(
|
|
|
|
|
file) : new ZipDir(new JarFile(file))
|
|
|
|
|
: null;
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|