Files
SCM-Manager/scm-webapp/src/main/java/sonia/scm/plugin/ScriptResourceServlet.java

127 lines
2.6 KiB
Java
Raw Normal View History

2010-09-16 21:50:30 +02:00
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sonia.scm.plugin;
//~--- non-JDK imports --------------------------------------------------------
import com.google.inject.Inject;
import com.google.inject.Singleton;
import sonia.scm.util.Util;
2010-09-20 14:51:10 +02:00
import sonia.scm.web.ScmWebPlugin;
import sonia.scm.web.ScmWebPluginContext;
import sonia.scm.web.WebResource;
2010-09-16 21:50:30 +02:00
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
2010-09-20 14:51:10 +02:00
import java.util.Collection;
2010-09-16 21:50:30 +02:00
import java.util.List;
import javax.servlet.ServletException;
/**
*
* @author Sebastian Sdorra
*/
@Singleton
public class ScriptResourceServlet extends AbstractResourceServlet
{
/** Field description */
public static final String CONTENT_TYPE = "text/javascript";
/** Field description */
private static final long serialVersionUID = -5769146163848821050L;
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param stream
*
* @throws IOException
* @throws ServletException
*/
@Override
protected void appendResources(OutputStream stream)
throws ServletException, IOException
{
stream.write(
"function sayPluginHello(){ alert('Plugin Hello !'); }".concat(
System.getProperty("line.separator")).getBytes());
2010-09-20 14:51:10 +02:00
Collection<WebResource> scriptResources =
webPluginContext.getScriptResources();
2010-09-16 21:50:30 +02:00
2010-09-20 14:51:10 +02:00
if (Util.isNotEmpty(scriptResources))
2010-09-16 21:50:30 +02:00
{
2010-09-20 14:51:10 +02:00
for (WebResource scriptResource : scriptResources)
2010-09-16 21:50:30 +02:00
{
2010-09-20 14:51:10 +02:00
appendResource(stream, scriptResource);
2010-09-16 21:50:30 +02:00
}
}
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
protected String getContentType()
{
return CONTENT_TYPE;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param stream
2010-09-20 14:51:10 +02:00
* @param script
2010-09-16 21:50:30 +02:00
*
* @throws IOException
* @throws ServletException
*/
2010-09-20 14:51:10 +02:00
private void appendResource(OutputStream stream, WebResource script)
2010-09-16 21:50:30 +02:00
throws ServletException, IOException
{
2010-09-20 14:51:10 +02:00
InputStream input = script.getContent();
2010-09-16 21:50:30 +02:00
if (input != null)
{
try
{
Util.copy(input, stream);
}
finally
{
Util.close(input);
}
}
}
//~--- fields ---------------------------------------------------------------
/** Field description */
@Inject
private ScmWebPluginContext webPluginContext;
}