Search in sources :

Example 1 with RequestCycle

use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.

the class FilenameWithVersionResourceCachingStrategy method undecorateUrl.

@Override
public void undecorateUrl(ResourceUrl url) {
    final String filename = url.getFileName();
    // check for extension
    int pos = filename.lastIndexOf('.');
    // get name of file without extension (but with version string)
    final String fullname = pos == -1 ? filename : filename.substring(0, pos);
    // get extension of file if present
    final String extension = pos == -1 ? null : filename.substring(pos);
    // get position of version string
    pos = fullname.lastIndexOf(versionPrefix);
    // remove version string if it exists
    if (pos != -1 && isVersion(fullname.substring(pos + versionPrefix.length()))) {
        // get filename before version string
        final String basename = fullname.substring(0, pos);
        // create filename without version string
        // (required for working resource lookup)
        url.setFileName(extension == null ? basename : basename + extension);
        // store the version in the request cycle
        RequestCycle requestCycle = RequestCycle.get();
        if (requestCycle != null) {
            int idx = fullname.indexOf(versionPrefix);
            String urlVersion = fullname.substring(idx + versionPrefix.length());
            requestCycle.setMetaData(URL_VERSION, urlVersion);
        }
    }
}
Also used : RequestCycle(org.apache.wicket.request.cycle.RequestCycle)

Example 2 with RequestCycle

use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.

the class DynamicJQueryResourceReference method getName.

@Override
public String getName() {
    RequestCycle requestCycle = RequestCycle.get();
    String name = requestCycle.getMetaData(KEY);
    if (name == null) {
        WebClientInfo clientInfo;
        name = getVersion2();
        if (Session.exists()) {
            WebSession session = WebSession.get();
            clientInfo = session.getClientInfo();
        } else {
            clientInfo = new WebClientInfo(requestCycle);
        }
        ClientProperties clientProperties = clientInfo.getProperties();
        if (clientProperties.isBrowserInternetExplorer() && clientProperties.getBrowserVersionMajor() < 9) {
            name = getVersion1();
        }
        requestCycle.setMetaData(KEY, name);
    }
    return name;
}
Also used : WebClientInfo(org.apache.wicket.protocol.http.request.WebClientInfo) ClientProperties(org.apache.wicket.protocol.http.ClientProperties) WebSession(org.apache.wicket.protocol.http.WebSession) RequestCycle(org.apache.wicket.request.cycle.RequestCycle)

Example 3 with RequestCycle

use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.

the class FileSystemResource method createResourceResponse.

/**
 * Creates a resource response based on the given attributes
 *
 * @param path
 *            the path to create the resource response with
 * @param attributes
 *            request attributes
 * @return the actual resource response
 */
protected ResourceResponse createResourceResponse(Attributes attributes, Path path) {
    try {
        if (path == null) {
            throw new WicketRuntimeException("Please override #newResourceResponse() and provide a path if using a constructor which doesn't take one as argument.");
        }
        this.path = new PathModel(path);
        long size = getSize();
        ResourceResponse resourceResponse = new ResourceResponse();
        resourceResponse.setContentType(getMimeType());
        resourceResponse.setAcceptRange(ContentRangeType.BYTES);
        resourceResponse.setContentLength(size);
        if (path.getFileName() != null) {
            resourceResponse.setFileName(path.getFileName().toString());
        }
        RequestCycle cycle = RequestCycle.get();
        Long startbyte = cycle.getMetaData(CONTENT_RANGE_STARTBYTE);
        Long endbyte = cycle.getMetaData(CONTENT_RANGE_ENDBYTE);
        resourceResponse.setWriteCallback(new PartWriterCallback(getInputStream(), size, startbyte, endbyte).setClose(true));
        return resourceResponse;
    } catch (IOException e) {
        throw new WicketRuntimeException("An error occurred while processing the media resource response", e);
    }
}
Also used : RequestCycle(org.apache.wicket.request.cycle.RequestCycle) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) IOException(java.io.IOException) PartWriterCallback(org.apache.wicket.request.resource.PartWriterCallback)

Example 4 with RequestCycle

use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.

the class PackageResourceReference method getResource.

/**
 * @see org.apache.wicket.request.resource.ResourceReference#getResource()
 */
@Override
public PackageResource getResource() {
    final String extension = getExtension();
    final PackageResource resource;
    RequestCycle requestCycle = RequestCycle.get();
    UrlAttributes urlAttributes = null;
    if (requestCycle != null) {
        // resource attributes (locale, style, variation) might be encoded in the URL
        final Url url = requestCycle.getRequest().getUrl();
        urlAttributes = ResourceUtil.decodeResourceReferenceAttributes(url);
    }
    final String currentVariation = getCurrentVariation(urlAttributes);
    final String currentStyle = getCurrentStyle(urlAttributes);
    final Locale currentLocale = getCurrentLocale(urlAttributes);
    final Class<?> scope = getScope();
    final String name = getName();
    if (CSS_EXTENSION.equals(extension)) {
        resource = new CssPackageResource(scope, name, currentLocale, currentStyle, currentVariation);
    } else if (JAVASCRIPT_EXTENSION.equals(extension)) {
        resource = new JavaScriptPackageResource(scope, name, currentLocale, currentStyle, currentVariation);
    } else {
        resource = new PackageResource(scope, name, currentLocale, currentStyle, currentVariation);
    }
    resource.readBuffered(readBuffered);
    removeCompressFlagIfUnnecessary(resource);
    return resource;
}
Also used : Locale(java.util.Locale) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) Url(org.apache.wicket.request.Url)

Example 5 with RequestCycle

use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.

the class RequestCycleCachedResourceVersion method getVersion.

@Override
public String getVersion(IStaticCacheableResource resource) {
    // get current request cycle
    final RequestCycle requestCycle = ThreadContext.getRequestCycle();
    // cache instance
    Map<Serializable, String> cache = null;
    // cache key
    Serializable key = null;
    // is request cycle available?
    if (requestCycle != null) {
        // retrieve cache from current request cycle
        cache = requestCycle.getMetaData(CACHE_KEY);
        // create caching key
        key = resource.getCacheKey();
        // does cache exist within current request cycle?
        if (cache == null) {
            // no, so create it
            requestCycle.setMetaData(CACHE_KEY, cache = Generics.newHashMap());
        } else if (cache.containsKey(key)) {
            // lookup timestamp from cache (may contain NULL values which are valid)
            return cache.get(key);
        }
    }
    // no cache entry found, query version from delegate
    final String version = delegate.getVersion(resource);
    // store value in cache (if it is available)
    if (cache != null && key != null) {
        cache.put(key, version);
    }
    return version;
}
Also used : Serializable(java.io.Serializable) RequestCycle(org.apache.wicket.request.cycle.RequestCycle)

Aggregations

RequestCycle (org.apache.wicket.request.cycle.RequestCycle)69 WebResponse (org.apache.wicket.request.http.WebResponse)14 IRequestHandler (org.apache.wicket.request.IRequestHandler)9 Url (org.apache.wicket.request.Url)9 Response (org.apache.wicket.request.Response)8 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)7 BufferedWebResponse (org.apache.wicket.protocol.http.BufferedWebResponse)7 Page (org.apache.wicket.Page)6 Request (org.apache.wicket.request.Request)6 RenderPageRequestHandler (org.apache.wicket.core.request.handler.RenderPageRequestHandler)5 IRequestCycle (org.apache.wicket.request.IRequestCycle)5 AbstractRequestCycleListener (org.apache.wicket.request.cycle.AbstractRequestCycleListener)5 Test (org.junit.Test)5 Application (org.apache.wicket.Application)4 IHeaderResponse (org.apache.wicket.markup.head.IHeaderResponse)4 WebClientInfo (org.apache.wicket.protocol.http.request.WebClientInfo)4 WebRequest (org.apache.wicket.request.http.WebRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 WebApplication (org.apache.wicket.protocol.http.WebApplication)3 UrlRenderer (org.apache.wicket.request.UrlRenderer)3