All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.myfaces.renderkit.html.util.MyFacesResourceHandler Maven / Gradle / Ivy

Go to download

JSF components and utilities that can be used with any JSF implementation. This library is compatible with both JSF1.1 and JSF1.2; however for JSF1.2 users there is an alternative build of Tomahawk available that takes advantage of JSF1.2 features to offer some additional benefits.

There is a newer version: 1.1.14
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.myfaces.renderkit.html.util;

import javax.faces.context.FacesContext;

/**
 * Class whose instances represent a resource inside the tomahawk jarfile
 * which a custom component needs to tell a browser to fetch.
 * 
 * @author Mathias Broekelmann
 */
public class MyFacesResourceHandler implements ResourceHandler
{
    private final Class _myfacesCustomComponent;
    private final String _resource;

    /**
     * Constructor.
     * 
     * @param myfacesCustomComponent is a class that must be in package
     *   org.apache.myfaces.custom. The resource to be served will be
     *   located relative to this class in the classpath. Note that code
     *   wishing to serve resources from other locations in the classpath
     *   must write a custom ResourceHandler implementation.
     *   
     * @param resourceName is the name of a file that can be found in dir
     *  "resource/{resourceName} relative to the location of the specified
     *  component class in the classpath.
     */
    public MyFacesResourceHandler(Class myfacesCustomComponent, String resourceName)
    {
        validateCustomComponent(myfacesCustomComponent);
        _myfacesCustomComponent = myfacesCustomComponent;
        _resource = resourceName;
    }
    
    /**
     * Return a Class object which can decode the url generated by this
     * class in the getResourceUri method and use that info to locate
     * the resource data represented by this object.
     * 
     * @see ResourceHandler#getResourceLoaderClass()
     */
    public Class getResourceLoaderClass()
    {
        return MyFacesResourceLoader.class;
    }

    /**
     * Verify that the base class for the resource lookup is in the 
     * org.apache.myfaces.custom package.
     * 
     * @param myfacesCustomComponent is the base component for the lookup.
     * @throws IllegalArgumentException if the class is not in the expected package. 
     */
    protected void validateCustomComponent(Class myfacesCustomComponent)
    {
        if (!myfacesCustomComponent.getName().startsWith(
                MyFacesResourceLoader.ORG_APACHE_MYFACES_CUSTOM + "."))
        {
            throw new IllegalArgumentException(
                    "expected a myfaces custom component class in package "
                            + MyFacesResourceLoader.ORG_APACHE_MYFACES_CUSTOM);
        }
    }

    /**
     * Return a URL that the browser can later submit to retrieve the resource
     * handled by this instance.
     * 

* The emitted URL is of form: *

     *   {partial.class.name}/{resourceName}
     * 
* where partial.class.name is the name of the base class specified in the * constructor, and resourceName is the resource specified in the constructor. * * @see org.apache.myfaces.shared.renderkit.html.util.ResourceHandler#getResourceUri(javax.faces.context.FacesContext) */ public String getResourceUri(FacesContext context) { String className = _myfacesCustomComponent.getName(); StringBuffer sb = new StringBuffer(); sb.append(className.substring( MyFacesResourceLoader.ORG_APACHE_MYFACES_CUSTOM.length() + 1)); sb.append("/"); if (_resource != null) { sb.append(_resource); } return sb.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy