/*
* Copyright 2017 OmniFaces
*
* Licensed 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.omnifaces.resourcehandler;
import java.io.Externalizable;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import javax.faces.FacesException;
import javax.faces.application.Resource;
import javax.faces.context.FacesContext;
/**
* This {@link Resource} implementation remaps the given resource to the given request path.
*
* @author Bauke Scholtz
* @since 2.1
*/
public class RemappedResource extends Resource implements Externalizable {
private Serializable serializableResource;
private transient Resource resource;
private String resourceName;
private String libraryName;
private String requestPath;
/**
* Do not use this constructor.
*/
public RemappedResource() {
// Keep default c'tor alive for Externalizable.
}
/**
* Constructs a new resource which remaps the given wrapped resource to the given request path.
* @param resource The resource to be remapped.
* @param requestPath The remapped request path.
*/
public RemappedResource(Resource resource, String requestPath) {
if (resource instanceof Serializable) {
serializableResource = (Serializable) resource;
}
this.resource = resource;
this.requestPath = requestPath;
}
/**
* Constructs a new resource which remaps the given requested resource and library name to the given request path.
* @param resourceName The requested resource name.
* @param libraryName The requested library name.
* @param requestPath The remapped request path.
*/
public RemappedResource(String resourceName, String libraryName, String requestPath) {
this.resourceName = resourceName;
this.libraryName = libraryName;
this.requestPath = requestPath;
}
@Override
public String getResourceName() {
return (resource != null) ? resource.getResourceName() : resourceName;
}
@Override
public String getLibraryName() {
return (resource != null) ? resource.getLibraryName() : libraryName;
}
@Override
public String getContentType() {
return (resource != null) ? resource.getContentType() : null;
}
@Override
public String getRequestPath() {
return requestPath;
}
@Override
public InputStream getInputStream() throws IOException {
return (resource != null) ? resource.getInputStream() : getURL().openStream();
}
@Override
public Map<String, String> getResponseHeaders() {
return (resource != null) ? resource.getResponseHeaders() : Collections.<String, String>emptyMap();
}
@Override
public URL getURL() {
try {
return (resource != null) ? resource.getURL() : new URL(requestPath);
}
catch (MalformedURLException e) {
throw new FacesException(e);
}
}
@Override
public boolean userAgentNeedsUpdate(FacesContext context) {
return (resource != null) ? resource.userAgentNeedsUpdate(context) : false;
}
@Override
public boolean equals(Object object) {
if (object == this) {
return true;
}
if (object == null || getClass() != object.getClass()) {
return false;
}
RemappedResource other = (RemappedResource) object;
return Objects.equals(resource, other.resource) && requestPath.equals(other.requestPath);
}
@Override
public int hashCode() {
return Objects.hash(resource, requestPath);
}
@Override
public void readExternal(ObjectInput input) throws IOException, ClassNotFoundException {
serializableResource = (Serializable) input.readObject();
resource = (Resource) serializableResource;
resourceName = (String) input.readObject();
libraryName = (String) input.readObject();
requestPath = (String) input.readObject();
}
@Override
public void writeExternal(ObjectOutput output) throws IOException {
output.writeObject(serializableResource);
output.writeObject(resourceName);
output.writeObject(libraryName);
output.writeObject(requestPath);
}
}