Example usage for org.apache.wicket Application exists

List of usage examples for org.apache.wicket Application exists

Introduction

In this page you can find the example usage for org.apache.wicket Application exists.

Prototype

public static boolean exists() 

Source Link

Document

Checks if the Application threadlocal is set in this thread

Usage

From source file:org.cast.cwm.dav.DavResource.java

License:Open Source License

/**
 * @see org.apache.wicket.request.resource.AbstractResource#newResourceResponse(org.apache.wicket.request.resource.IResource.Attributes)
 *//*  ww w .j a  v a  2s.  co m*/
@Override
protected ResourceResponse newResourceResponse(final Attributes attributes) {
    final ResourceResponse response = new ResourceResponse();

    // Set last modified time so that response can determine if data needs to be written.
    response.setLastModified(lastModifiedTime());

    if (response.dataNeedsToBeWritten(attributes)) {
        DavResourceStream resourceStream = new DavResourceStream();
        final byte[] bytes;
        try {
            bytes = IOUtils.toByteArray(resourceStream.getInputStream());
            response.setContentLength(bytes.length);
            response.setLastModified(lastModified);
            if (Application.exists())
                response.setContentType(Application.get().getMimeType(path));

            response.setWriteCallback(new WriteCallback() {
                @Override
                public void writeData(Attributes attributes) {
                    attributes.getResponse().write(bytes);
                }
            });
        } catch (IOException e) {
            log.error("I/O Exception while trying to read DAV resource");
            e.printStackTrace();
            response.setError(404);
        } catch (ResourceStreamNotFoundException e) {
            log.error("DAV resource not found: " + path);
            response.setError(404);
        } finally {
            try {
                resourceStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    return response;
}

From source file:org.cast.cwm.ResourceDirectory.java

License:Open Source License

/**
 * creates a new resource response based on the request attributes
 * /*from  w  w w .ja v a  2 s. c o m*/
 * @param attributes
 *            current request attributes from client
 * @return resource response for answering request
 */
@Override
protected ResourceResponse newResourceResponse(Attributes attributes) {
    String relativePath = "";
    PageParameters parameters = attributes.getParameters();
    for (int i = 0; i < parameters.getIndexedCount(); i++) {
        relativePath += parameters.get(i);
        relativePath += '/';
    }
    if (relativePath.endsWith("/"))
        relativePath = relativePath.substring(0, relativePath.length() - 1);
    log.trace("relativePath is {}", relativePath);

    String absolutePath = new File(sourceDirectory, relativePath).getAbsolutePath();

    final ResourceResponse resourceResponse = new ResourceResponse();

    final IResourceStream resourceStream = getResourceStream(absolutePath);

    // bail out if resource stream could not be found
    if (resourceStream == null) {
        return sendResourceError(absolutePath, resourceResponse, HttpServletResponse.SC_NOT_FOUND,
                "Unable to find resource");
    }

    // allow caching
    resourceResponse.setCacheScope(WebResponse.CacheScope.PUBLIC);
    resourceResponse.setCacheDuration(cacheDuration);

    // add Last-Modified header (to support HEAD requests and If-Modified-Since)
    resourceResponse.setLastModified(resourceStream.lastModifiedTime());

    if (resourceResponse.dataNeedsToBeWritten(attributes)) {
        String contentType = resourceStream.getContentType();

        if (contentType == null && Application.exists())
            contentType = Application.get().getMimeType(absolutePath);

        // set Content-Type (may be null)
        resourceResponse.setContentType(contentType);

        try {
            // read resource data
            final byte[] bytes;

            bytes = IOUtils.toByteArray(resourceStream.getInputStream());

            // send Content-Length header
            resourceResponse.setContentLength(bytes.length);

            // send response body with resource data
            resourceResponse.setWriteCallback(new WriteCallback() {
                @Override
                public void writeData(Attributes attributes) {
                    attributes.getResponse().write(bytes);
                }
            });
        } catch (IOException e) {
            log.debug(e.getMessage(), e);
            return sendResourceError(absolutePath, resourceResponse, 500, "Unable to read resource stream");
        } catch (ResourceStreamNotFoundException e) {
            log.debug(e.getMessage(), e);
            return sendResourceError(absolutePath, resourceResponse, 500, "Unable to open resource stream");
        } finally {
            try {
                resourceStream.close();
            } catch (IOException e) {
                log.warn("Unable to close the resource stream", e);
            }
        }
    }

    return resourceResponse;
}

From source file:org.hippoecm.frontend.plugin.impl.PluginContext.java

License:Apache License

private void writeObject(ObjectOutputStream output) throws IOException {
    if (stopping && Application.exists()) {
        if (Application.get().getConfigurationType().equals(RuntimeConfigurationType.DEVELOPMENT)) {
            throw new WicketRuntimeException("Stopped plugin is still being referenced: "
                    + (plugin != null ? plugin.getClass().getName() : "unknown"));
        }/*from w  w  w. j a v  a  2  s .c  o  m*/
    }
    output.defaultWriteObject();
}

From source file:org.hippoecm.frontend.session.PluginUserSession.java

License:Apache License

public void flush() {
    Task flushTask = null;/*  w  ww.j a v  a  2s  .co  m*/

    try {
        if (HDC.isStarted()) {
            flushTask = HDC.getCurrentTask().startSubtask("PluginUserSession.flush");
        }

        JcrObservationManager.getInstance().cleanupListeners(this);
        if (Application.exists()) {
            clear();
        } else {
            log.info("There is no wicket application attached to the current thread.");
        }
    } finally {
        if (flushTask != null) {
            flushTask.stop();
        }
    }
}

From source file:org.projectforge.web.wicket.WicketApplicationFilter.java

License:Open Source License

public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
        throws IOException, ServletException {
    // Sollte eigentlich immer NULL ergeben, aber man weiss nie ...
    final Application previousOne = (Application.exists() == true) ? Application.get() : null;
    org.apache.wicket.ThreadContext.setApplication(this.application);
    try {//www.ja v a2s.  c  o m
        chain.doFilter(request, response);
    } finally {
        if (previousOne != null) {
            org.apache.wicket.ThreadContext.setApplication(previousOne);
        } else {
            org.apache.wicket.ThreadContext.setApplication(null);
        }
    }
}

From source file:org.wicketstuff.lazymodel.reflect.CachingMethodResolver.java

License:Apache License

private IMethodResolver getResolver() {
    Object key;/*from ww  w  .  j a  v  a  2 s  .  c om*/
    if (Application.exists()) {
        key = Application.get();
    } else {
        key = CachingMethodResolver.class;
    }

    IMethodResolver result = scopes.get(key);
    if (result == null) {
        IMethodResolver tmpResult = scopes.putIfAbsent(key, result = new ApplicationScope());
        if (tmpResult != null) {
            result = tmpResult;
        }
    }

    return result;
}

From source file:org.wicketstuff.lazymodel.reflect.CachingProxyFactory.java

License:Apache License

private IProxyFactory getFactory() {
    Object key;/*from   w  ww .  j a va2  s. co m*/
    if (Application.exists()) {
        key = Application.get();
    } else {
        key = CachingProxyFactory.class;
    }

    IProxyFactory result = scopes.get(key);
    if (result == null) {
        IProxyFactory tmpResult = scopes.putIfAbsent(key, result = new ApplicationScope());
        if (tmpResult != null) {
            result = tmpResult;
        }
    }

    return result;
}

From source file:org.wicketstuff.whiteboard.WhiteboardBehavior.java

License:Apache License

private static WhiteboardLibrarySettings getLibrarySettings() {
    if (Application.exists()
            && (Application.get().getJavaScriptLibrarySettings() instanceof WhiteboardLibrarySettings)) {
        return (WhiteboardLibrarySettings) Application.get().getJavaScriptLibrarySettings();
    }//from  w  ww  . j  a  va 2s. com

    return null;
}

From source file:sk.drunkenpanda.leaflet.Leaflet.java

License:Apache License

/**
 * Returns settings of application, that belongs to current thread.
 *
 * @return settings of application in current thread
 * @throws IllegalStateException if there isn't any application in current thread
 *///from  w  ww.j ava  2 s .  co m
public static LeafletSettings getSettings() {
    // get settings for application in current thread
    if (Application.exists()) {
        Application app = Application.get();
        return getSettings(app);
    }
    throw new IllegalStateException("Application was not found in current thread");
}