001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.component;
018
019import java.io.ByteArrayInputStream;
020import java.io.IOException;
021import java.io.InputStream;
022
023import org.apache.camel.Component;
024import org.apache.camel.api.management.ManagedAttribute;
025import org.apache.camel.api.management.ManagedOperation;
026import org.apache.camel.api.management.ManagedResource;
027import org.apache.camel.api.management.mbean.ManagedResourceEndpointMBean;
028import org.apache.camel.converter.IOConverter;
029import org.apache.camel.impl.ProcessorEndpoint;
030import org.apache.camel.spi.Metadata;
031import org.apache.camel.spi.UriParam;
032import org.apache.camel.spi.UriPath;
033import org.apache.camel.util.IOHelper;
034import org.apache.camel.util.ResourceHelper;
035import org.slf4j.Logger;
036import org.slf4j.LoggerFactory;
037
038/**
039 * A useful base class for endpoints which depend on a resource
040 * such as things like Velocity or XQuery based components.
041 */
042@ManagedResource(description = "Managed ResourceEndpoint")
043public abstract class ResourceEndpoint extends ProcessorEndpoint implements ManagedResourceEndpointMBean {
044    protected final Logger log = LoggerFactory.getLogger(getClass());
045    private volatile byte[] buffer;
046    @UriPath(description = "Path to the resource, or a reference to lookup a bean in the Registry to use as the resource") @Metadata(required = "true")
047    private String resourceUri;
048    @UriParam(defaultValue = "false", description = "Sets whether to use resource content cache or not")
049    private boolean contentCache;
050    @UriParam(defaultValue = "false", description = "Sets whether the context map should allow access to all details."
051            + " By default only the message body and headers can be accessed."
052            + " This option can be enabled for full access to the current Exchange and CamelContext."
053            + " Doing so impose a potential security risk as this opens access to the full power of CamelContext API.")
054    private boolean allowContextMapAll;
055
056    public ResourceEndpoint() {
057    }
058
059    public ResourceEndpoint(String endpointUri, Component component, String resourceUri) {
060        super(endpointUri, component);
061        this.resourceUri = resourceUri;
062    }
063
064    /**
065     * Gets the resource as an input stream considering the cache flag as well.
066     * <p/>
067     * If cache is enabled then the resource content is cached in an internal buffer and this content is
068     * returned to avoid loading the resource over and over again.
069     *
070     * @return the input stream
071     * @throws IOException is thrown if error loading the content of the resource to the local cache buffer
072     */
073    public InputStream getResourceAsInputStream() throws IOException {
074        // try to get the resource input stream
075        InputStream is;
076        if (isContentCache()) {
077            synchronized (this) {
078                if (buffer == null) {
079                    log.debug("Reading resource: {} into the content cache", resourceUri);
080                    is = getResourceAsInputStreamWithoutCache();
081                    buffer = IOConverter.toBytes(is);
082                    IOHelper.close(is, resourceUri, log);
083                }
084            }
085            log.debug("Using resource: {} from the content cache", resourceUri);
086            return new ByteArrayInputStream(buffer);
087        }
088
089        return getResourceAsInputStreamWithoutCache();
090    }
091
092    protected InputStream getResourceAsInputStreamWithoutCache() throws IOException {
093        return loadResource(resourceUri);
094    }
095
096    /**
097     * Loads the given resource.
098     *
099     * @param uri uri of the resource.
100     * @return the loaded resource
101     * @throws IOException is thrown if resource is not found or cannot be loaded
102     */
103    protected InputStream loadResource(String uri) throws IOException {
104        return ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext(), uri);
105    }
106
107    @ManagedAttribute(description = "Whether the resource is cached")
108    public boolean isContentCache() {
109        return contentCache;
110    }
111
112    @ManagedOperation(description = "Clears the cached resource, forcing to re-load the resource on next request")
113    public void clearContentCache() {
114        log.debug("Clearing resource: {} from the content cache", resourceUri);
115        buffer = null;
116    }
117
118    public boolean isContentCacheCleared() {
119        return buffer == null;
120    }
121
122    @ManagedAttribute(description = "Whether the context map is limited to only include the message body and headers")
123    public boolean isAllowContextMapAll() {
124        return allowContextMapAll;
125    }
126
127    /**
128     * Sets whether the context map should allow access to all details.
129     * By default only the message body and headers can be accessed.
130     * This option can be enabled for full access to the current Exchange and CamelContext.
131     * Doing so impose a potential security risk as this opens access to the full power of CamelContext API.
132     */
133    public void setAllowContextMapAll(boolean allowContextMapAll) {
134        this.allowContextMapAll = allowContextMapAll;
135    }
136
137    @ManagedAttribute(description = "Camel context ID")
138    public String getCamelId() {
139        return getCamelContext().getName();
140    }
141
142    @ManagedAttribute(description = "Camel ManagementName")
143    public String getCamelManagementName() {
144        return getCamelContext().getManagementName();
145    }
146
147    @ManagedAttribute(description = "Endpoint service state")
148    public String getState() {
149        return getStatus().name();
150    }
151
152    /**
153     * Sets whether to use resource content cache or not.
154     */
155    public void setContentCache(boolean contentCache) {
156        this.contentCache = contentCache;
157    }
158
159    public String getResourceUri() {
160        return resourceUri;
161    }
162
163    /**
164     * Path to the resource, or a reference to lookup a bean in the Registry to use as the resource
165     *
166     * @param resourceUri  the resource path
167     */
168    public void setResourceUri(String resourceUri) {
169        this.resourceUri = resourceUri;
170    }
171}