Example usage for org.apache.wicket.protocol.http WebApplication getServletContext

List of usage examples for org.apache.wicket.protocol.http WebApplication getServletContext

Introduction

In this page you can find the example usage for org.apache.wicket.protocol.http WebApplication getServletContext.

Prototype

public ServletContext getServletContext() 

Source Link

Document

Gets the servlet context for this application.

Usage

From source file:at.molindo.wicketutils.utils.MockUtils.java

License:Apache License

/**
 * reuse an existing session if possible
 *///from   ww w . ja  va  2s  . co  m
public static <V> V withRequest(WebApplication webApplication, IMockRequestCallback<V> callback) {
    Session oldSession = ThreadContext.exists() ? ThreadContext.getSession() : null;
    ThreadContext oldContext = ThreadContext.detach();

    try {
        ThreadContext.setApplication(webApplication);
        ThreadContext.setSession(oldSession);

        // mock http session
        ServletContext context = webApplication.getServletContext();
        MockHttpSession httpSession = new MockHttpSession(context);

        // mock servlet request
        MockServletRequest servletRequest = new MockServletRequest(webApplication, httpSession, context);
        callback.configure(new MockRequest(servletRequest));
        servletRequest.setDefaultHeaders();

        // mock response
        MockHttpServletResponse servletResponse = new MockHttpServletResponse(servletRequest);

        // mock web request
        final WebRequest request = VisibilityHelper.newWebRequest(webApplication, servletRequest, "/");

        // mock web response
        final WebResponse response = VisibilityHelper.newWebResponse(webApplication, request, servletResponse);

        // create
        ThreadContext.setRequestCycle(webApplication.createRequestCycle(request, response));

        return callback.call();
    } finally {
        Session newSession = ThreadContext.getSession();
        ThreadContext.restore(oldContext);
        if (oldSession == null && newSession != null && !newSession.isTemporary()) {
            // reuse session if a new one was created
            ThreadContext.setSession(newSession);
        }
    }
}

From source file:com.evolveum.midpoint.web.page.self.component.LinksPanel.java

License:Apache License

@Override
protected void initLayout() {

    final List<RichHyperlinkType> linksList = getModel().getObject();
    RepeatingView rowView = new RepeatingView(ID_LINKS_ROW);

    int linksListSize = linksList == null ? 0 : linksList.size();
    if (linksListSize > 0) {
        int currentColumn = 0;
        RepeatingView columnView = null;
        WebMarkupContainer row = null;//ww w  .  j  a  v  a 2  s . c  o  m
        boolean isRowAdded = false;
        for (int i = 0; i < linksListSize; i++) {
            final RichHyperlinkType link = linksList.get(i);
            if (WebComponentUtil.isAuthorized(link.getAuthorization())) {
                if (currentColumn == 0) {
                    row = new WebMarkupContainer(rowView.newChildId());
                    isRowAdded = false;
                    columnView = new RepeatingView(ID_LINKS_COLUMN);
                }
                WebMarkupContainer column = new WebMarkupContainer(columnView.newChildId());
                Link linkItem = new Link(ID_LINK) {
                    @Override
                    public void onClick() {
                    }

                    @Override
                    protected void onComponentTag(final ComponentTag tag) {
                        super.onComponentTag(tag);
                        String rootContext = "";
                        //TODO: what is this for???
                        if (link.getTargetUrl() != null && !link.getTargetUrl().startsWith("http://")
                                && !link.getTargetUrl().startsWith("https://")
                                && !link.getTargetUrl().startsWith("www://")
                                && !link.getTargetUrl().startsWith("//")) {
                            WebApplication webApplication = WebApplication.get();
                            if (webApplication != null) {
                                ServletContext servletContext = webApplication.getServletContext();
                                if (servletContext != null) {
                                    rootContext = servletContext.getContextPath();
                                }
                            }
                        }
                        tag.put("href",
                                rootContext + (link.getTargetUrl() == null ? "#" : link.getTargetUrl()));
                    }
                };
                linkItem.add(new Label(ID_IMAGE) {
                    @Override
                    protected void onComponentTag(final ComponentTag tag) {
                        super.onComponentTag(tag);
                        String cssClass = ICON_DEFAULT_CSS_CLASS;
                        if (link.getIcon() != null) {
                            cssClass = link.getIcon().getCssClass();
                        }
                        tag.put("class",
                                "info-box-icon "
                                        + (link.getColor() != null
                                                ? (link.getColor().startsWith("bg-") ? link.getColor()
                                                        : "bg-" + link.getColor())
                                                : "")
                                        + " " + cssClass);
                    }
                });

                linkItem.add(new Label(ID_LABEL, new Model<String>() {
                    public String getObject() {
                        return link.getLabel();
                    }
                }));
                Label description = new Label(ID_DESCRIPTION, new Model<String>() {
                    public String getObject() {
                        return link.getDescription();
                    }
                });
                description.setEnabled(false);
                linkItem.add(description);

                column.add(linkItem);
                columnView.add(column);
                if (currentColumn == 1 || (linksList.indexOf(link) == linksListSize - 1)) {
                    row.add(columnView);
                    rowView.add(row);
                    currentColumn = 0;
                    isRowAdded = true;
                } else {
                    currentColumn++;
                }
            } else {
                LOGGER.trace("Link {} not authorized, skipping", link);
            }
        }
        if (row != null && columnView != null && !isRowAdded) {
            row.add(columnView);
            rowView.add(row);
        }
    }
    add(rowView);
}

From source file:com.rex.crm.TemplatePage.java

License:Apache License

public static String getRootContext() {

    String rootContext = "";

    WebApplication webApplication = WebApplication.get();

    if (webApplication != null) {
        ServletContext servletContext = webApplication.getServletContext();
        if (servletContext != null) {
            rootContext = servletContext.getServletContextName();
        } else {//ww  w  . jav a  2s  . c  o m
            //do nothing
        }
    } else {
        //do nothing
    }

    return rootContext;

}

From source file:de.alpharogroup.wicket.base.util.application.ApplicationExtensions.java

License:Apache License

/**
 * Adds the given resourcePath to the resource finder from the given application.
 *
 * @param application//from  w ww. jav a 2s  .  com
 *            the application
 * @param resourcePath
 *            the resource path
 * @see org.apache.wicket.settings.ResourceSettings#getResourceFinders()
 */
public static void addResourceFinder(final WebApplication application, final String resourcePath) {
    application.getResourceSettings().getResourceFinders()
            .add(new WebApplicationPath(application.getServletContext(), resourcePath));
}

From source file:de.alpharogroup.wicket.base.util.application.ApplicationExtensions.java

License:Apache License

/**
 * Gets the context path from the given WebApplication.
 *
 * @param application//from ww  w .j  a  v  a 2 s. c  o m
 *            the WebApplication
 * @return the context path
 */
public static String getContextPath(final WebApplication application) {
    final String contextPath = application.getServletContext().getContextPath();
    if ((null != contextPath) && !contextPath.isEmpty()) {
        return contextPath;
    }
    return "";
}

From source file:de.alpharogroup.wicket.base.util.application.ApplicationExtensions.java

License:Apache License

/**
 * Gets the real path corresponding to the given virtual path from the given WebApplication.
 * This method gets decorated the method of the
 * {@link javax.servlet.ServletContext#getRealPath(String)}.
 *
 * @param application//from  ww  w. j  a v  a 2 s. c om
 *            the wicket application
 * @param path
 *            the virtual path to be translated to a real path
 * @return the real path, or null if the translation cannot be performed
 */

public static String getRealPath(final WebApplication application, final String path) {
    final String realPath = application.getServletContext().getRealPath(path);
    if ((null != realPath) && !realPath.isEmpty()) {
        return realPath;
    }
    return "";
}

From source file:de.alpharogroup.wicket.base.util.application.ApplicationExtensions.java

License:Apache License

/**
 * Use this method to enable hot deploy of your html templates on development. Works only with
 * jetty. Only for/*from   w  w w  .  j  a  va 2  s . c  o  m*/
 *
 * @param application
 *            the new html hot deploy
 */
public static void setHtmlHotDeploy(final WebApplication application) {
    application.getResourceSettings().setResourcePollFrequency(Duration.ONE_SECOND);
    final String slash = "/";
    String realPath = application.getServletContext().getRealPath(slash);
    if ((realPath != null) && !realPath.endsWith(slash)) {
        realPath += slash;
    }
    final String javaSourcePath = realPath + "../java";
    final String resourcesPath = realPath + "../resources";
    addResourceFinder(application, javaSourcePath);
    addResourceFinder(application, resourcesPath);
}

From source file:de.alpharogroup.wicket.base.util.WicketComponentExtensions.java

License:Apache License

/**
 * Gets the context path from the given WebApplication.
 *
 * @param application//w ww. ja  va  2 s  . com
 *            the WebApplication
 * @return the context path
 * @deprecated use instead {@link ApplicationExtensions#getContextPath(WebApplication)}
 */
@Deprecated
public static String getContextPath(final WebApplication application) {
    final String contextPath = application.getServletContext().getContextPath();
    if ((null != contextPath) && !contextPath.isEmpty()) {
        return contextPath;
    }
    return "";
}

From source file:org.apache.openmeetings.db.util.ApplicationHelper.java

License:Apache License

public static WicketTester getWicketTester(long langId) {
    WebApplication app = (WebApplication) ensureApplication(langId);

    WicketTester tester = new WicketTester(app, app.getServletContext());
    InitializationContainer.initComplete = true;
    return tester;
}

From source file:org.apache.openmeetings.db.util.ApplicationHelper.java

License:Apache License

public static IApplication ensureApplication(Long langId) {
    IApplication a = null;//w  w  w .  j  ava 2s .  c  o  m
    if (Application.exists()) {
        a = (IApplication) Application.get();
    } else {
        WebApplication app = (WebApplication) Application.get(wicketApplicationName);
        LabelDao.initLanguageMap();
        if (app == null) {
            try {
                app = (WebApplication) LabelDao.getAppClass().newInstance();
            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                log.error("Failed to create Application");
                return null;
            }
            app.setServletContext(new MockServletContext(app, null));
            app.setName(wicketApplicationName);
            ServletContext sc = app.getServletContext();
            OMContextListener omcl = new OMContextListener();
            omcl.contextInitialized(new ServletContextEvent(sc));
            XmlWebApplicationContext xmlContext = new XmlWebApplicationContext();
            xmlContext.setConfigLocation("classpath:openmeetings-applicationContext.xml");
            xmlContext.setServletContext(sc);
            xmlContext.refresh();
            sc.setAttribute(ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, xmlContext);
            app.setConfigurationType(RuntimeConfigurationType.DEPLOYMENT);
            ThreadContext.setApplication(app);
            app.initApplication();
        } else {
            ThreadContext.setApplication(app);
        }
        a = (IApplication) Application.get(wicketApplicationName);
    }
    if (ThreadContext.getRequestCycle() == null) {
        ServletWebRequest req = new ServletWebRequest(new MockHttpServletRequest((Application) a,
                new MockHttpSession(a.getServletContext()), a.getServletContext()), "");
        RequestCycleContext rctx = new RequestCycleContext(req, new MockWebResponse(), a.getRootRequestMapper(),
                a.getExceptionMapperProvider().get());
        ThreadContext.setRequestCycle(new RequestCycle(rctx));
    }
    if (ThreadContext.getSession() == null) {
        WebSession s = WebSession.get();
        if (langId > 0) {
            ((IWebSession) s).setLanguage(langId);
        }
    }
    return a;
}