Modified: ace/trunk/org.apache.ace.repository/src/org/apache/ace/repository/servlet/RepositoryServlet.java URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.repository/src/org/apache/ace/repository/servlet/RepositoryServlet.java?rev=1726821&r1=1726820&r2=1726821&view=diff ============================================================================== --- ace/trunk/org.apache.ace.repository/src/org/apache/ace/repository/servlet/RepositoryServlet.java (original) +++ ace/trunk/org.apache.ace.repository/src/org/apache/ace/repository/servlet/RepositoryServlet.java Tue Jan 26 15:19:17 2016 @@ -20,54 +20,30 @@ package org.apache.ace.repository.servle import java.io.IOException; import java.io.InputStream; -import java.util.Dictionary; import org.apache.ace.range.SortedRangeSet; import org.apache.ace.repository.Repository; -import org.osgi.framework.InvalidSyntaxException; -import org.osgi.framework.ServiceReference; -import org.osgi.service.cm.ConfigurationException; -public class RepositoryServlet extends RepositoryServletBase { +public class RepositoryServlet extends RepositoryServletBase<Repository> { private static final long serialVersionUID = 1L; - - @Override - protected ServiceReference[] getRepositories(String filter) throws InvalidSyntaxException { - return m_context.getServiceReferences(Repository.class.getName(), filter); - } - - @Override - protected SortedRangeSet getRange(ServiceReference ref) throws IOException { - Repository repository = (Repository) m_context.getService(ref); - SortedRangeSet range = repository.getRange(); - m_context.ungetService(ref); - return range; - } - - @Override - protected boolean doCommit(ServiceReference ref, long version, InputStream data) throws IllegalArgumentException, IOException { - Repository r = (Repository) m_context.getService(ref); - boolean result = r.commit(data, version); - m_context.ungetService(ref); - return result; + + public RepositoryServlet() { + super(Repository.class); } @Override - protected InputStream doCheckout(ServiceReference ref, long version) throws IllegalArgumentException, IOException { - Repository r = (Repository) m_context.getService(ref); - InputStream result = r.checkout(version); - m_context.ungetService(ref); - return result; + public String getServletInfo() { + return "Apache ACE Repository Servlet"; } @Override - public String getServletInfo() { - return "Apache ACE Repository Servlet"; + protected InputStream doCheckout(Repository repo, long version) throws IllegalArgumentException, IOException { + return repo.checkout(version); } @Override - public void updated(Dictionary settings) throws ConfigurationException { - super.updated(settings); + protected boolean doCommit(Repository repo, long version, InputStream data) throws IllegalArgumentException, IOException { + return repo.commit(data, version); } @Override @@ -79,4 +55,9 @@ public class RepositoryServlet extends R protected String getCommitCommand() { return "/commit"; } + + @Override + protected SortedRangeSet getRange(Repository repo) throws IOException { + return repo.getRange(); + } } \ No newline at end of file
Modified: ace/trunk/org.apache.ace.repository/src/org/apache/ace/repository/servlet/RepositoryServletBase.java URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.repository/src/org/apache/ace/repository/servlet/RepositoryServletBase.java?rev=1726821&r1=1726820&r2=1726821&view=diff ============================================================================== --- ace/trunk/org.apache.ace.repository/src/org/apache/ace/repository/servlet/RepositoryServletBase.java (original) +++ ace/trunk/org.apache.ace.repository/src/org/apache/ace/repository/servlet/RepositoryServletBase.java Tue Jan 26 15:19:17 2016 @@ -23,7 +23,9 @@ import static javax.servlet.http.HttpSer import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.ArrayList; import java.util.Dictionary; +import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; @@ -46,7 +48,7 @@ import org.osgi.service.useradmin.User; * Base class for the repository servlets. Both the repository and the repository replication servlets work in a similar * way, so the specifics were factored out of this base class and put in two subclasses. */ -public abstract class RepositoryServletBase extends HttpServlet implements ManagedService { +public abstract class RepositoryServletBase<REPO_TYPE> extends HttpServlet implements ManagedService { /** A boolean denoting whether or not authentication is enabled. */ private static final String KEY_USE_AUTHENTICATION = "authentication.enabled"; private static final int COPY_BUFFER_SIZE = 1024; @@ -54,6 +56,7 @@ public abstract class RepositoryServletB protected static final String TEXT_MIMETYPE = "text/plain"; protected static final String BINARY_MIMETYPE = "application/octet-stream"; + private final Class<REPO_TYPE> m_repoType; // injected by Dependency Manager private volatile DependencyManager m_dm; private volatile AuthenticationService m_authService; @@ -61,6 +64,53 @@ public abstract class RepositoryServletB protected volatile BundleContext m_context; protected volatile LogService m_log; + public RepositoryServletBase(Class<REPO_TYPE> repoType) { + m_repoType = repoType; + } + + public void updated(Dictionary settings) throws ConfigurationException { + if (settings != null) { + String useAuthString = (String) settings.get(KEY_USE_AUTHENTICATION); + if ((useAuthString == null) || + !("true".equalsIgnoreCase(useAuthString) || "false".equalsIgnoreCase(useAuthString))) { + throw new ConfigurationException(KEY_USE_AUTHENTICATION, "Missing or invalid value!"); + } + boolean useAuth = Boolean.parseBoolean(useAuthString); + m_useAuth = useAuth; + } + else { + m_useAuth = false; + } + } + + /** + * Checkout or get data from the repository. + * + * @param repo + * the repository service + * @param version + * the version to check out. + * @return the data + * @throws IllegalArgumentException + * @throws java.io.IOException + */ + protected abstract InputStream doCheckout(REPO_TYPE repo, long version) throws IllegalArgumentException, IOException; + + /** + * Commit or put the data into the repository. + * + * @param repo + * the repository service + * @param version + * The version to commit + * @param data + * The data + * @return <code>true</code> if successful + * @throws IllegalArgumentException + * @throws IOException + */ + protected abstract boolean doCommit(REPO_TYPE repo, long version, InputStream data) throws IllegalArgumentException, IOException; + @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path = request.getPathInfo(); @@ -106,11 +156,6 @@ public abstract class RepositoryServletB } } - /** - * Returns the name of the "checkout" command. - */ - protected abstract String getCheckoutCommand(); - @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path = request.getPathInfo(); @@ -134,64 +179,31 @@ public abstract class RepositoryServletB } /** - * Returns the name of the "commit" command. + * Returns the name of the "checkout" command. */ - protected abstract String getCommitCommand(); + protected abstract String getCheckoutCommand(); /** - * Handles a query command and sends back the response. + * Returns the name of the "commit" command. */ - private void handleQuery(String filter, HttpServletResponse response) throws IOException { - try { - ServiceReference[] refs = getRepositories(filter); - StringBuffer result = new StringBuffer(); - - if (refs != null) { - for (ServiceReference ref : refs) { - result.append((String) ref.getProperty("customer")); - result.append(','); - result.append((String) ref.getProperty("name")); - result.append(','); - result.append(getRange(ref).toRepresentation()); - result.append('\n'); - } - } - - response.setContentType(TEXT_MIMETYPE); - response.getWriter().print(result.toString()); - } - catch (IOException e) { - response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, - "Could not retrieve version range for repository: " + e.getMessage()); - } - catch (InvalidSyntaxException e) { - response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, - "Invalid filter syntax: " + e.getMessage()); - } - } + protected abstract String getCommitCommand(); /** * Implement this by asking the right repository for a range of available versions. * - * @param ref Reference to the repository service you need to dereference + * @param repo + * the repository service * @return a sorted range set - * @throws IOException If the range cannot be obtained - */ - protected abstract SortedRangeSet getRange(ServiceReference ref) throws IOException; - - /** - * Returns a list of repositories that match the specified filter condition. - * - * @param filter The filter condition - * @return An array of service references - * @throws InvalidSyntaxException If the filter condition is invalid + * @throws IOException + * If the range cannot be obtained */ - protected abstract ServiceReference[] getRepositories(String filter) throws InvalidSyntaxException; + protected abstract SortedRangeSet getRange(REPO_TYPE repo) throws IOException; /** * Called by Dependency Manager upon initialization of this component. * - * @param comp the component to initialize, cannot be <code>null</code>. + * @param comp + * the component to initialize, cannot be <code>null</code>. */ protected void init(Component comp) { comp.add(m_dm.createServiceDependency() @@ -214,7 +226,8 @@ public abstract class RepositoryServletB /** * Authenticates, if needed the user with the information from the given request. * - * @param request The request to obtain the credentials from, cannot be <code>null</code>. + * @param request + * The request to obtain the credentials from, cannot be <code>null</code>. * @return <code>true</code> if the authentication was successful, <code>false</code> otherwise. */ private boolean authenticate(HttpServletRequest request) { @@ -232,127 +245,179 @@ public abstract class RepositoryServletB } /** - * Handles a commit command and sends back the response. + * Copies data from an input stream to an output stream. + * + * @param in + * The input + * @param outThe + * output + * @param version + * @param name + * @throws IOException + * If copying fails */ - private void handleCommit(String customer, String name, long version, InputStream data, HttpServletResponse response) throws IOException { - try { - ServiceReference[] refs = getRepositories("(&(customer=" + customer + ")(name=" + name + "))"); - - if ((refs != null) && (refs.length == 1)) { - ServiceReference ref = refs[0]; - try { - if (!doCommit(ref, version, data)) { - response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not commit"); - } - else { - response.sendError(HttpServletResponse.SC_OK); - } - } - catch (IllegalArgumentException e) { - response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Invalid version"); - } - catch (IllegalStateException e) { - response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, - "Cannot commit, not the master repository"); - } - } - } - catch (IOException e) { - response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "I/O exception: " + e.getMessage()); - } - catch (InvalidSyntaxException e) { - response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Invalid filter syntax: " - + e.getMessage()); + private void copy(InputStream in, OutputStream out, String name, long version) + throws IOException { + byte[] buffer = new byte[COPY_BUFFER_SIZE]; + int bytes = in.read(buffer); + while (bytes != -1) { + out.write(buffer, 0, bytes); + bytes = in.read(buffer); } + } /** - * Commit or put the data into the repository. + * Returns a list of repositories that match the specified filter condition. * - * @param ref Reference to the repository service - * @param version The version - * @param data The data - * @return <code>true</code> if successful - * @throws IllegalArgumentException - * @throws IOException + * @param filter + * The filter condition + * @return An array of service references + * @throws InvalidSyntaxException + * If the filter condition is invalid */ - protected abstract boolean doCommit(ServiceReference ref, long version, InputStream data) throws IllegalArgumentException, IOException; + private List<ServiceReference<REPO_TYPE>> getRepositories(String filter) throws InvalidSyntaxException { + List<ServiceReference<REPO_TYPE>> result = new ArrayList<>(); + result.addAll(m_context.getServiceReferences(m_repoType, filter)); + return result; + } /** * Handles a checkout command and returns the response. */ private void handleCheckout(String customer, String name, long version, HttpServletResponse response) throws IOException { + List<ServiceReference<REPO_TYPE>> refs; + try { + refs = getRepositories("(&(customer=" + customer + ")(name=" + name + "))"); + } + catch (InvalidSyntaxException e) { + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Invalid filter syntax: " + e.getMessage()); + return; + } + try { - ServiceReference[] refs = getRepositories("(&(customer=" + customer + ")(name=" + name + "))"); - if ((refs != null) && (refs.length == 1)) { - ServiceReference ref = refs[0]; + if (refs.size() != 1) { + response.sendError(HttpServletResponse.SC_NOT_FOUND, + (refs.isEmpty() ? "Could not find repository " : "Multiple repositories found ") + " for customer " + customer + ", name " + name); + return; + } + + ServiceReference<REPO_TYPE> ref = refs.get(0); + if (ref == null) { + response.sendError(HttpServletResponse.SC_NOT_FOUND, "Could not find repository for customer " + customer + ", name " + name); + return; + } + + REPO_TYPE repo = m_context.getService(ref); + + try { response.setContentType(BINARY_MIMETYPE); - InputStream data = doCheckout(ref, version); + + InputStream data = doCheckout(repo, version); if (data == null) { - response.sendError(HttpServletResponse.SC_NOT_FOUND, "Requested version does not exist: " - + version); + response.sendError(HttpServletResponse.SC_NOT_FOUND, "Requested version does not exist: " + version); } else { copy(data, response.getOutputStream(), name, version); } } - else { - response.sendError(HttpServletResponse.SC_NOT_FOUND, - ((refs == null) ? "Could not find repository " : "Multiple repositories found ") + " for customer " - + customer + ", name " + name); + finally { + m_context.ungetService(ref); } } catch (IOException e) { response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "I/O exception: " + e.getMessage()); } - catch (InvalidSyntaxException e) { - response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Invalid filter syntax: " + e.getMessage()); - } } /** - * Checkout or get data from the repository. - * - * @param ref reference to the repository service - * @param version the version - * @return the data - * @throws IllegalArgumentException - * @throws java.io.IOException + * Handles a commit command and sends back the response. */ - protected abstract InputStream doCheckout(ServiceReference ref, long version) throws IllegalArgumentException, IOException; + private void handleCommit(String customer, String name, long version, InputStream data, HttpServletResponse response) throws IOException { + List<ServiceReference<REPO_TYPE>> refs; + try { + refs = getRepositories("(&(customer=" + customer + ")(name=" + name + "))"); + } + catch (InvalidSyntaxException e) { + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Invalid filter syntax: " + e.getMessage()); + return; + } + + try { + if (refs.size() != 1) { + response.sendError(HttpServletResponse.SC_NOT_FOUND, + (refs.isEmpty() ? "Could not find repository " : "Multiple repositories found ") + " for customer " + customer + ", name " + name); + return; + } + + ServiceReference<REPO_TYPE> ref = refs.get(0); + if (ref == null) { + response.sendError(HttpServletResponse.SC_NOT_FOUND, "Could not find repository for customer " + customer + ", name " + name); + return; + } + + REPO_TYPE repo = m_context.getService(ref); + + try { + if (!doCommit(repo, version, data)) { + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not commit"); + } + else { + response.sendError(HttpServletResponse.SC_OK); + } + } + catch (IllegalArgumentException e) { + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Invalid version"); + } + catch (IllegalStateException e) { + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Cannot commit, not the master repository"); + } + finally { + m_context.ungetService(ref); + } + } + catch (IOException e) { + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "I/O exception: " + e.getMessage()); + } + } /** - * Copies data from an input stream to an output stream. - * - * @param in The input - * @param outThe output - * @param version - * @param name - * @throws IOException If copying fails + * Handles a query command and sends back the response. */ - private void copy(InputStream in, OutputStream out, String name, long version) - throws IOException { - byte[] buffer = new byte[COPY_BUFFER_SIZE]; - int bytes = in.read(buffer); - while (bytes != -1) { - out.write(buffer, 0, bytes); - bytes = in.read(buffer); + private void handleQuery(String filter, HttpServletResponse response) throws IOException { + List<ServiceReference<REPO_TYPE>> refs; + try { + refs = getRepositories(filter); + } + catch (InvalidSyntaxException e) { + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Invalid filter syntax: " + e.getMessage()); + return; } - } + try { + StringBuffer result = new StringBuffer(); - public void updated(Dictionary settings) throws ConfigurationException { - if (settings != null) { - String useAuthString = (String) settings.get(KEY_USE_AUTHENTICATION); - if ((useAuthString == null) || - !("true".equalsIgnoreCase(useAuthString) || "false".equalsIgnoreCase(useAuthString))) { - throw new ConfigurationException(KEY_USE_AUTHENTICATION, "Missing or invalid value!"); + for (ServiceReference<REPO_TYPE> ref : refs) { + REPO_TYPE repo = m_context.getService(ref); + try { + result.append((String) ref.getProperty("customer")); + result.append(','); + result.append((String) ref.getProperty("name")); + result.append(','); + result.append(getRange(repo).toRepresentation()); + result.append('\n'); + } + finally { + m_context.ungetService(ref); + } } - boolean useAuth = Boolean.parseBoolean(useAuthString); - m_useAuth = useAuth; + + response.setContentType(TEXT_MIMETYPE); + response.getWriter().print(result.toString()); } - else { - m_useAuth = false; + catch (IOException e) { + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, + "Could not retrieve version range for repository: " + e.getMessage()); } } }
