/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.resource.temporary.script;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.xwiki.component.annotation.Component;
import org.xwiki.context.Execution;
import org.xwiki.resource.ResourceReferenceSerializer;
import org.xwiki.resource.script.ResourceScriptService;
import org.xwiki.resource.temporary.TemporaryResourceReference;
import org.xwiki.script.service.ScriptService;
import org.xwiki.stability.Unstable;
import org.xwiki.url.ExtendedURL;

/**
 * Exposes the Temporary Resource API to server-side scripts.
 * 
 * @version $Id$
 * @since 14.4.1
 * @since 14.5RC1
 */
@Component
@Named(TemporaryResourceScriptService.ROLE_HINT)
@Singleton
@Unstable
public class TemporaryResourceScriptService implements ScriptService
{
    /**
     * The role hint of this component.
     */
    public static final String ROLE_HINT = ResourceScriptService.ROLE_HINT + ".temporary";

    /**
     * The key under which the last encountered error is stored in the current execution context.
     */
    private static final String TEMP_RESOURCE_ERROR_KEY = String.format("scriptservice.%s.error", ROLE_HINT);

    /**
     * Used to obtain the URL that corresponds to a temporary resource.
     */
    @Inject
    @Named("standard/tmp")
    private ResourceReferenceSerializer<TemporaryResourceReference, ExtendedURL> urlTempResourceReferenceSerializer;

    /**
     * Provides access to the current context, used to store and retrieve caught exceptions.
     */
    @Inject
    private Execution execution;

    /**
     * @param tempResourceReference a temporary resource reference
     * @return the URL corresponding to the specified temporary resource
     */
    public String getURL(TemporaryResourceReference tempResourceReference)
    {
        setError(null);

        try {
            return this.urlTempResourceReferenceSerializer.serialize(tempResourceReference).serialize();
        } catch (Exception e) {
            setError(e);
            return null;
        }
    }

    /**
     * Get the error generated while performing the previously called action.
     *
     * @return an eventual exception or {@code null} if no exception was thrown
     */
    public Exception getLastError()
    {
        return (Exception) this.execution.getContext().getProperty(TEMP_RESOURCE_ERROR_KEY);
    }

    /**
     * Store a caught exception in the context, so that it can be later retrieved using {@link #getLastError()}.
     *
     * @param e the exception to store, can be {@code null} to clear the previously stored exception
     * @see #getLastError()
     */
    private void setError(Exception e)
    {
        this.execution.getContext().setProperty(TEMP_RESOURCE_ERROR_KEY, e);
    }
}

@@ -1276,24 +1276,84 @@
 eReference;%0A
+import org.xwiki.resource.temporary.TemporaryResourceStore;%0A
 import org.x
@@ -1379,32 +1379,32 @@
 .ScriptService;%0A
-
 import org.xwiki
@@ -2353,16 +2353,92 @@
 ializer;
+%0A    %0A    @Inject%0A    private TemporaryResourceStore temporaryResourceStore;
 %0A%0A    /*
@@ -3020,24 +3020,24 @@
 etError(e);%0A
-
             
@@ -3058,32 +3058,532 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * Check if a temporary resource exists.%0A     * %0A     * @param tempResourceReference a temporary resource reference%0A     * @return %7B@code true%7D if the specified temporary resource exists, %7B@code false%7D otherwise%0A     */%0A    public boolean exists(TemporaryResourceReference tempResourceReference)%0A    %7B%0A        try %7B%0A            return this.temporaryResourceStore.getTemporaryFile(tempResourceReference).exists();%0A        %7D catch (Exception e) %7B%0A            return false;%0A        %7D%0A    %7D%0A%0A
     /**%0A     * G

Add a script service API to check if a temporary resource exists