improve api

This commit is contained in:
Sebastian Sdorra
2010-09-16 21:50:30 +02:00
parent 16070397b0
commit 2f170494cf
13 changed files with 689 additions and 102 deletions

View File

@@ -0,0 +1,186 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sonia.scm.plugin;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.util.Util;
//~--- JDK imports ------------------------------------------------------------
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Sebastian Sdorra
*/
public abstract class AbstractResourceServlet extends HttpServlet
{
/** Field description */
public static final String DEFAULT_CHARSET = "UTF-8";
/** Field description */
private static final long serialVersionUID = -7703282364120349053L;
public AbstractResourceServlet()
{
System.out.println("CONSTRUCT !!!");
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param stream
*
* @throws IOException
* @throws ServletException
*/
protected abstract void appendResources(OutputStream stream)
throws ServletException, IOException;
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
protected abstract String getContentType();
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @throws ServletException
*/
@Override
public void init() throws ServletException
{
System.out.println("INIT !!!");
ByteArrayOutputStream output = new ByteArrayOutputStream();
try
{
appendResources(output);
}
catch (IOException ex)
{
throw new ServletException(ex);
}
finally
{
Util.close(output);
}
this.content = output.toByteArray();
}
/**
* Method description
*
*
* @param request
* @param response
*
* @throws IOException
* @throws ServletException
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
/**
* Method description
*
*
* @param request
* @param response
*
* @throws IOException
* @throws ServletException
*/
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
protected String getCharacterEncoding()
{
return DEFAULT_CHARSET;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param request
* @param response
*
* @throws IOException
* @throws ServletException
*/
private void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
System.out.println( "JA !!!" );
response.setCharacterEncoding(getCharacterEncoding());
response.setContentType(getContentType());
response.setContentLength(content.length);
OutputStream output = response.getOutputStream();
try
{
output.write(content);
}
finally
{
Util.close(output);
}
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private byte[] content;
}

View File

@@ -0,0 +1,123 @@
/*
* 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.ScmWebPlugin;
import sonia.scm.ScmWebPluginContext;
import sonia.scm.util.Util;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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());
List<ScmWebPlugin> plugins = webPluginContext.getPlugins();
if (Util.isNotEmpty(plugins))
{
for (ScmWebPlugin plugin : plugins)
{
appendResource(stream, plugin);
}
}
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
protected String getContentType()
{
return CONTENT_TYPE;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param stream
* @param plugin
*
* @throws IOException
* @throws ServletException
*/
private void appendResource(OutputStream stream, ScmWebPlugin plugin)
throws ServletException, IOException
{
InputStream input = plugin.getScript();
if (input != null)
{
try
{
Util.copy(input, stream);
}
finally
{
Util.close(input);
}
}
}
//~--- fields ---------------------------------------------------------------
/** Field description */
@Inject
private ScmWebPluginContext webPluginContext;
}