merge with feature/ui-extensions branch

This commit is contained in:
Sebastian Sdorra
2018-08-30 12:15:17 +02:00
1508 changed files with 29291 additions and 203236 deletions

View File

@@ -0,0 +1,24 @@
package sonia.scm;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* This dispatcher forwards every request to the index.html of the application.
*
* @since 2.0.0
*/
public class ForwardingPushStateDispatcher implements PushStateDispatcher {
@Override
public void dispatch(HttpServletRequest request, HttpServletResponse response, String uri) throws IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("/index.html");
try {
dispatcher.forward(request, response);
} catch (ServletException e) {
throw new IOException("failed to forward request", e);
}
}
}

View File

@@ -0,0 +1,132 @@
package sonia.scm;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.io.ByteStreams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
/**
* PushStateDispatcher which delegates the request to a different server. This dispatcher should only be used for
* development and never in production.
*
* @since 2.0.0
*/
public final class ProxyPushStateDispatcher implements PushStateDispatcher {
private static final Logger LOG = LoggerFactory.getLogger(ProxyPushStateDispatcher.class);
@FunctionalInterface
interface ConnectionFactory {
HttpURLConnection open(URL url) throws IOException;
}
private final String target;
private final ConnectionFactory connectionFactory;
/**
* Creates a new dispatcher for the given target. The target must be a valid url.
*
* @param target proxy target
*/
public ProxyPushStateDispatcher(String target) {
this(target, ProxyPushStateDispatcher::openConnection);
}
/**
* This Constructor should only be used for testing.
*
* @param target proxy target
* @param connectionFactory factory for creating an connection from a url
*/
@VisibleForTesting
ProxyPushStateDispatcher(String target, ConnectionFactory connectionFactory) {
this.target = target;
this.connectionFactory = connectionFactory;
}
@Override
public void dispatch(HttpServletRequest request, HttpServletResponse response, String uri) throws IOException {
URL url = createProxyUrl(uri);
HttpURLConnection connection = connectionFactory.open(url);
connection.setRequestMethod(request.getMethod());
copyRequestHeaders(request, connection);
if (request.getContentLength() > 0) {
copyRequestBody(request, connection);
}
int responseCode = connection.getResponseCode();
response.setStatus(responseCode);
copyResponseHeaders(response, connection);
if (connection.getContentLength() > 0) {
copyResponseBody(response, connection);
}
}
private void copyResponseBody(HttpServletResponse response, HttpURLConnection connection) throws IOException {
try (InputStream input = getConnectionInput(connection); OutputStream output = response.getOutputStream()) {
ByteStreams.copy(input, output);
}
}
private InputStream getConnectionInput(HttpURLConnection connection) throws IOException {
if (connection.getErrorStream() != null) {
return connection.getErrorStream();
}
return connection.getInputStream();
}
private void copyResponseHeaders(HttpServletResponse response, HttpURLConnection connection) {
Map<String, List<String>> headerFields = connection.getHeaderFields();
for (Map.Entry<String, List<String>> entry : headerFields.entrySet()) {
if (entry.getKey() != null && !"Transfer-Encoding".equalsIgnoreCase(entry.getKey())) {
for (String value : entry.getValue()) {
response.addHeader(entry.getKey(), value);
}
}
}
}
private void copyRequestBody(HttpServletRequest request, HttpURLConnection connection) throws IOException {
connection.setDoOutput(true);
try (InputStream input = request.getInputStream(); OutputStream output = connection.getOutputStream()) {
ByteStreams.copy(input, output);
}
}
private void copyRequestHeaders(HttpServletRequest request, HttpURLConnection connection) {
Enumeration<String> headers = request.getHeaderNames();
while (headers.hasMoreElements()) {
String header = headers.nextElement();
Enumeration<String> values = request.getHeaders(header);
while (values.hasMoreElements()) {
String value = values.nextElement();
connection.setRequestProperty(header, value);
}
}
}
private URL createProxyUrl(String uri) throws MalformedURLException {
return new URL(target + uri);
}
private static HttpURLConnection openConnection(URL url) throws IOException {
return (HttpURLConnection) url.openConnection();
}
}

View File

@@ -0,0 +1,28 @@
package sonia.scm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* The PushStateDispatcher is responsible for dispatching the request, to the main entry point of the ui, if no resource
* could be found for the requested path. This allows us the implementation of a ui which work with "pushstate" of
* html5.
*
* @since 2.0.0
* @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/History_API">HTML5 Push State</a>
*/
public interface PushStateDispatcher {
/**
* Dispatches the request to the main entry point of the ui.
*
* @param request http request
* @param response http response
* @param uri request uri
*
* @throws IOException
*/
void dispatch(HttpServletRequest request, HttpServletResponse response, String uri) throws IOException;
}

View File

@@ -0,0 +1,28 @@
package sonia.scm;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import javax.inject.Provider;
/**
* Injection Provider for the {@link PushStateDispatcher}. The provider will return a {@link ProxyPushStateDispatcher}
* if the system property {@code PushStateDispatcherProvider#PROPERTY_TARGET} is set to a proxy target url, otherwise
* a {@link ForwardingPushStateDispatcher} is used.
*
* @since 2.0.0
*/
public class PushStateDispatcherProvider implements Provider<PushStateDispatcher> {
@VisibleForTesting
static final String PROPERTY_TARGET = "sonia.scm.ui.proxy";
@Override
public PushStateDispatcher get() {
String target = System.getProperty(PROPERTY_TARGET);
if (Strings.isNullOrEmpty(target)) {
return new ForwardingPushStateDispatcher();
}
return new ProxyPushStateDispatcher(target);
}
}

View File

@@ -63,10 +63,6 @@ import sonia.scm.repository.api.HookContextFactory;
import sonia.scm.repository.api.RepositoryServiceFactory;
import sonia.scm.repository.spi.HookEventFacade;
import sonia.scm.repository.xml.XmlRepositoryDAO;
import sonia.scm.resources.DefaultResourceManager;
import sonia.scm.resources.DevelopmentResourceManager;
import sonia.scm.resources.ResourceManager;
import sonia.scm.resources.ScriptResourceServlet;
import sonia.scm.schedule.QuartzScheduler;
import sonia.scm.schedule.Scheduler;
import sonia.scm.security.*;
@@ -266,16 +262,6 @@ public class ScmServletModule extends ServletModule
transformers.addBinding().to(JsonContentTransformer.class);
bind(AdvancedHttpClient.class).to(DefaultAdvancedHttpClient.class);
// bind resourcemanager
if (context.getStage() == Stage.DEVELOPMENT)
{
bind(ResourceManager.class, DevelopmentResourceManager.class);
}
else
{
bind(ResourceManager.class, DefaultResourceManager.class);
}
// bind repository service factory
bind(RepositoryServiceFactory.class);
@@ -295,9 +281,6 @@ public class ScmServletModule extends ServletModule
// debug servlet
serve(PATTERN_DEBUG).with(DebugServlet.class);
// plugin resources
serve(PATTERN_PLUGIN_SCRIPT).with(ScriptResourceServlet.class);
// template
serve(PATTERN_INDEX, "/").with(TemplateServlet.class);
@@ -313,7 +296,7 @@ public class ScmServletModule extends ServletModule
// bind events
// bind(LastModifiedUpdateListener.class);
bind(PushStateDispatcher.class).toProvider(PushStateDispatcherProvider.class);
}

View File

@@ -0,0 +1,85 @@
package sonia.scm;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.io.Resources;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.filter.WebElement;
import sonia.scm.plugin.PluginLoader;
import sonia.scm.plugin.UberWebResourceLoader;
import sonia.scm.util.HttpUtil;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
/**
* WebResourceServlet serves resources from the {@link UberWebResourceLoader}.
*
* @since 2.0.0
*/
@Singleton
@WebElement(value = WebResourceServlet.PATTERN, regex = true)
public class WebResourceServlet extends HttpServlet {
/**
* exclude api requests and the old frontend servlets.
*
* TODO remove old frontend servlets
*/
@VisibleForTesting
static final String PATTERN = "/(?!api/).*";
private static final Logger LOG = LoggerFactory.getLogger(WebResourceServlet.class);
private final UberWebResourceLoader webResourceLoader;
private final PushStateDispatcher pushStateDispatcher;
@Inject
public WebResourceServlet(PluginLoader pluginLoader, PushStateDispatcher dispatcher) {
this.webResourceLoader = pluginLoader.getUberWebResourceLoader();
this.pushStateDispatcher = dispatcher;
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
String uri = normalizeUri(request);
LOG.trace("try to load {}", uri);
URL url = webResourceLoader.getResource(uri);
if (url != null) {
serveResource(response, url);
} else {
dispatch(request, response, uri);
}
}
private void dispatch(HttpServletRequest request, HttpServletResponse response, String uri) {
try {
pushStateDispatcher.dispatch(request, response, uri);
} catch (IOException ex) {
LOG.error("failed to dispatch: " + uri, ex);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
private String normalizeUri(HttpServletRequest request) {
return HttpUtil.getStrippedURI(request);
}
private void serveResource(HttpServletResponse response, URL url) {
// TODO lastModifiedDate, if-... ???
try (OutputStream output = response.getOutputStream()) {
Resources.copy(url, output);
} catch (IOException ex) {
LOG.warn("failed to serve resource: {}", url);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
}

View File

@@ -30,6 +30,10 @@ public class MapperModule extends AbstractModule {
bind(FileObjectToFileObjectDtoMapper.class).to(Mappers.getMapper(FileObjectToFileObjectDtoMapper.class).getClass());
// no mapstruct required
bind(UIPluginDtoMapper.class);
bind(UIPluginDtoCollectionMapper.class);
bind(UriInfoStore.class).in(ServletScopes.REQUEST);
}
}

View File

@@ -349,4 +349,37 @@ class ResourceLinks {
return permissionLinkBuilder.method("getRepositoryResource").parameters(repositoryNamespace, repositoryName).method("permissions").parameters().method(methodName).parameters(permissionName).href();
}
}
public UIPluginLinks uiPlugin() {
return new UIPluginLinks(uriInfoStore.get());
}
static class UIPluginLinks {
private final LinkBuilder uiPluginLinkBuilder;
UIPluginLinks(UriInfo uriInfo) {
uiPluginLinkBuilder = new LinkBuilder(uriInfo, UIRootResource.class, UIPluginResource.class);
}
String self(String id) {
return uiPluginLinkBuilder.method("plugins").parameters().method("getInstalledPlugin").parameters(id).href();
}
}
public UIPluginCollectionLinks uiPluginCollection() {
return new UIPluginCollectionLinks(uriInfoStore.get());
}
static class UIPluginCollectionLinks {
private final LinkBuilder uiPluginCollectionLinkBuilder;
UIPluginCollectionLinks(UriInfo uriInfo) {
uiPluginCollectionLinkBuilder = new LinkBuilder(uriInfo, UIRootResource.class, UIPluginResource.class);
}
String self() {
return uiPluginCollectionLinkBuilder.method("plugins").parameters().method("getInstalledPlugins").parameters().href();
}
}
}

View File

@@ -0,0 +1,25 @@
package sonia.scm.api.v2.resources;
import de.otto.edison.hal.HalRepresentation;
import de.otto.edison.hal.Links;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter @Setter @NoArgsConstructor
public class UIPluginDto extends HalRepresentation {
private String name;
private Iterable<String> bundles;
public UIPluginDto(String name, Iterable<String> bundles) {
this.name = name;
this.bundles = bundles;
}
@Override
protected HalRepresentation add(Links links) {
return super.add(links);
}
}

View File

@@ -0,0 +1,46 @@
package sonia.scm.api.v2.resources;
import com.google.inject.Inject;
import de.otto.edison.hal.Embedded;
import de.otto.edison.hal.HalRepresentation;
import de.otto.edison.hal.Links;
import sonia.scm.plugin.PluginWrapper;
import java.util.Collection;
import java.util.List;
import static de.otto.edison.hal.Embedded.embeddedBuilder;
import static de.otto.edison.hal.Links.linkingTo;
import static java.util.stream.Collectors.toList;
public class UIPluginDtoCollectionMapper {
private final ResourceLinks resourceLinks;
private final UIPluginDtoMapper mapper;
@Inject
public UIPluginDtoCollectionMapper(ResourceLinks resourceLinks, UIPluginDtoMapper mapper) {
this.resourceLinks = resourceLinks;
this.mapper = mapper;
}
public HalRepresentation map(Collection<PluginWrapper> plugins) {
List<UIPluginDto> dtos = plugins.stream().map(mapper::map).collect(toList());
return new HalRepresentation(createLinks(), embedDtos(dtos));
}
private Links createLinks() {
String baseUrl = resourceLinks.uiPluginCollection().self();
Links.Builder linksBuilder = linkingTo()
.with(Links.linkingTo().self(baseUrl).build());
return linksBuilder.build();
}
private Embedded embedDtos(List<UIPluginDto> dtos) {
return embeddedBuilder()
.with("plugins", dtos)
.build();
}
}

View File

@@ -0,0 +1,61 @@
package sonia.scm.api.v2.resources;
import com.google.common.base.Strings;
import de.otto.edison.hal.Links;
import sonia.scm.plugin.PluginWrapper;
import sonia.scm.util.HttpUtil;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;
import static de.otto.edison.hal.Links.linkingTo;
public class UIPluginDtoMapper {
private final ResourceLinks resourceLinks;
private final HttpServletRequest request;
@Inject
public UIPluginDtoMapper(ResourceLinks resourceLinks, HttpServletRequest request) {
this.resourceLinks = resourceLinks;
this.request = request;
}
public UIPluginDto map(PluginWrapper plugin) {
UIPluginDto dto = new UIPluginDto(
plugin.getPlugin().getInformation().getName(),
getScriptResources(plugin)
);
Links.Builder linksBuilder = linkingTo()
.self(resourceLinks.uiPlugin()
.self(plugin.getId()));
dto.add(linksBuilder.build());
return dto;
}
private Set<String> getScriptResources(PluginWrapper wrapper) {
Set<String> scriptResources = wrapper.getPlugin().getResources().getScriptResources();
if (scriptResources != null) {
return scriptResources.stream()
.map(this::addContextPath)
.collect(Collectors.toSet());
}
return Collections.emptySet();
}
private String addContextPath(String resource) {
String ctxPath = request.getContextPath();
if (Strings.isNullOrEmpty(ctxPath)) {
return resource;
}
return HttpUtil.append(ctxPath, resource);
}
}

View File

@@ -0,0 +1,90 @@
package sonia.scm.api.v2.resources;
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
import com.webcohesion.enunciate.metadata.rs.TypeHint;
import sonia.scm.plugin.PluginLoader;
import sonia.scm.plugin.PluginWrapper;
import sonia.scm.web.VndMediaType;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class UIPluginResource {
private final PluginLoader pluginLoader;
private final UIPluginDtoCollectionMapper collectionMapper;
private final UIPluginDtoMapper mapper;
@Inject
public UIPluginResource(PluginLoader pluginLoader, UIPluginDtoCollectionMapper collectionMapper, UIPluginDtoMapper mapper) {
this.pluginLoader = pluginLoader;
this.collectionMapper = collectionMapper;
this.mapper = mapper;
}
/**
* Returns a collection of installed plugins and their ui bundles.
*
* @return collection of installed plugins.
*/
@GET
@Path("")
@StatusCodes({
@ResponseCode(code = 200, condition = "success"),
@ResponseCode(code = 500, condition = "internal server error")
})
@TypeHint(CollectionDto.class)
@Produces(VndMediaType.UI_PLUGIN_COLLECTION)
public Response getInstalledPlugins() {
List<PluginWrapper> plugins = pluginLoader.getInstalledPlugins()
.stream()
.filter(this::filter)
.collect(Collectors.toList());
return Response.ok(collectionMapper.map(plugins)).build();
}
/**
* Returns the installed plugin with the given id.
*
* @param id id of plugin
*
* @return installed plugin with specified id
*/
@GET
@Path("{id}")
@StatusCodes({
@ResponseCode(code = 200, condition = "success"),
@ResponseCode(code = 404, condition = "not found"),
@ResponseCode(code = 500, condition = "internal server error")
})
@TypeHint(UIPluginDto.class)
@Produces(VndMediaType.UI_PLUGIN)
public Response getInstalledPlugin(@PathParam("id") String id) {
Optional<UIPluginDto> uiPluginDto = pluginLoader.getInstalledPlugins()
.stream()
.filter(this::filter)
.filter(plugin -> id.equals(plugin.getId()))
.map(mapper::map)
.findFirst();
if (uiPluginDto.isPresent()) {
return Response.ok(uiPluginDto.get()).build();
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
}
private boolean filter(PluginWrapper plugin) {
return plugin.getPlugin().getResources() != null;
}
}

View File

@@ -0,0 +1,22 @@
package sonia.scm.api.v2.resources;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.ws.rs.Path;
@Path("v2/ui")
public class UIRootResource {
private Provider<UIPluginResource> uiPluginResourceProvider;
@Inject
public UIRootResource(Provider<UIPluginResource> uiPluginResourceProvider) {
this.uiPluginResourceProvider = uiPluginResourceProvider;
}
@Path("plugins")
public UIPluginResource plugins() {
return uiPluginResourceProvider.get();
}
}

View File

@@ -62,7 +62,8 @@ import javax.servlet.http.HttpServletResponse;
* @author Sebastian Sdorra
*/
@Priority(Filters.PRIORITY_AUTHORIZATION)
@WebElement(value = Filters.PATTERN_RESTAPI, morePatterns = { Filters.PATTERN_DEBUG })
// TODO find a better way for unprotected resources
@WebElement(value = "/api/rest/(?!v2/ui).*", regex = true)
public class SecurityFilter extends HttpFilter
{

View File

@@ -39,18 +39,18 @@ import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//~--- JDK imports ------------------------------------------------------------
import javax.servlet.ServletContext;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import javax.servlet.ServletContext;
//~--- JDK imports ------------------------------------------------------------
/**
* Default implementation of the {@link UberWebResourceLoader}.
@@ -133,7 +133,7 @@ public class DefaultUberWebResourceLoader implements UberWebResourceLoader
try
{
URL ctxResource = servletContext.getResource(path);
URL ctxResource = nonDirectory(servletContext.getResource(path));
if (ctxResource != null)
{
@@ -143,7 +143,7 @@ public class DefaultUberWebResourceLoader implements UberWebResourceLoader
for (PluginWrapper wrapper : plugins)
{
URL resource = wrapper.getWebResourceLoader().getResource(path);
URL resource = nonDirectory(wrapper.getWebResourceLoader().getResource(path));
if (resource != null)
{
@@ -185,17 +185,17 @@ public class DefaultUberWebResourceLoader implements UberWebResourceLoader
*/
private URL find(String path)
{
URL resource = null;
URL resource;
try
{
resource = servletContext.getResource(path);
resource = nonDirectory(servletContext.getResource(path));
if (resource == null)
{
for (PluginWrapper wrapper : plugins)
{
resource = wrapper.getWebResourceLoader().getResource(path);
resource = nonDirectory(wrapper.getWebResourceLoader().getResource(path));
if (resource != null)
{
@@ -218,6 +218,29 @@ public class DefaultUberWebResourceLoader implements UberWebResourceLoader
return resource;
}
private URL nonDirectory(URL url) {
if (url == null) {
return null;
}
if (isDirectory(url)) {
return null;
}
return url;
}
private boolean isDirectory(URL url) {
if ("file".equals(url.getProtocol())) {
try {
return Files.isDirectory(Paths.get(url.toURI()));
} catch (URISyntaxException ex) {
throw Throwables.propagate(ex);
}
}
return false;
}
//~--- fields ---------------------------------------------------------------
/** Field description */

View File

@@ -93,7 +93,7 @@ public class PathWebResourceLoader implements WebResourceLoader
URL resource = null;
Path file = directory.resolve(filePath(path));
if (Files.exists(file))
if (Files.exists(file) && ! Files.isDirectory(file))
{
logger.trace("found path {} at {}", path, file);

View File

@@ -1,215 +0,0 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.resources;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.io.Resources;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.plugin.PluginLoader;
import sonia.scm.util.Util;
//~--- JDK imports ------------------------------------------------------------
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.Collections;
import java.util.List;
/**
*
* @author Sebastian Sdorra
*/
public abstract class AbstractResource implements Resource
{
/**
* the logger for AbstractResource
*/
private static final Logger logger =
LoggerFactory.getLogger(AbstractResource.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
* @param pluginLoader
* @param resources
* @param resourceHandlers
*/
public AbstractResource(PluginLoader pluginLoader, List<String> resources,
List<ResourceHandler> resourceHandlers)
{
this.pluginLoader = pluginLoader;
this.resources = resources;
this.resourceHandlers = resourceHandlers;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param stream
*
* @throws IOException
*/
protected void appendResources(OutputStream stream) throws IOException
{
if (Util.isNotEmpty(resources))
{
for (String resource : resources)
{
appendResource(stream, resource);
}
}
if (Util.isNotEmpty(resourceHandlers))
{
Collections.sort(resourceHandlers, new ResourceHandlerComparator());
for (ResourceHandler resourceHandler : resourceHandlers)
{
processResourceHandler(stream, resourceHandler);
}
}
}
/**
* Method description
*
*
* @param stream
* @param path
*
* @throws IOException
*/
private void appendResource(OutputStream stream, String path)
throws IOException
{
URL resource = getResourceAsURL(path);
if (resource != null)
{
Resources.copy(resource, stream);
}
else if (logger.isWarnEnabled())
{
logger.warn("could not find resource {}", path);
}
}
/**
* Method description
*
*
* @param stream
* @param resourceHandler
*
* @throws IOException
*/
private void processResourceHandler(OutputStream stream,
ResourceHandler resourceHandler)
throws IOException
{
if (resourceHandler.getType() == getType())
{
if (logger.isTraceEnabled())
{
logger.trace("process resource handler {}", resourceHandler.getClass());
}
URL resource = resourceHandler.getResource();
if (resource != null)
{
Resources.copy(resource, stream);
}
else if (logger.isDebugEnabled())
{
logger.debug("resource handler {} does not return a resource",
resourceHandler.getClass());
}
}
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param path
*
* @return
*/
private URL getResourceAsURL(String path)
{
URL resource = null;
ClassLoader classLoader = pluginLoader.getUberClassLoader();
if (classLoader != null)
{
String classLoaderResource = path;
if (classLoaderResource.startsWith("/"))
{
classLoaderResource = classLoaderResource.substring(1);
}
resource = classLoader.getResource(classLoaderResource);
}
return resource;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
protected final List<ResourceHandler> resourceHandlers;
/** Field description */
protected final List<String> resources;
/** Field description */
private final PluginLoader pluginLoader;
}

View File

@@ -1,308 +0,0 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.resources;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.plugin.Plugin;
import sonia.scm.plugin.PluginLoader;
import sonia.scm.plugin.PluginResources;
//~--- JDK imports ------------------------------------------------------------
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.servlet.ServletContext;
import sonia.scm.plugin.PluginWrapper;
/**
*
* @author Sebastian Sdorra
*/
public abstract class AbstractResourceManager implements ResourceManager
{
/**
* Constructs ...
*
* @param pluginLoader
* @param resourceHandlers
*/
protected AbstractResourceManager(PluginLoader pluginLoader,
Set<ResourceHandler> resourceHandlers)
{
this.pluginLoader = pluginLoader;
this.resourceHandlers = resourceHandlers;
collectResources(resourceMap);
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param resourceMap
*/
protected abstract void collectResources(Map<ResourceKey,
Resource> resourceMap);
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param type
* @param name
*
* @return
*/
@Override
public Resource getResource(ResourceType type, String name)
{
return resourceMap.get(new ResourceKey(name, type));
}
/**
* Method description
*
*
* @param type
*
* @return
*/
@Override
public List<Resource> getResources(ResourceType type)
{
List<Resource> resources = new ArrayList<>();
for (Entry<ResourceKey, Resource> e : resourceMap.entrySet())
{
if (e.getKey().getType() == type)
{
resources.add(e.getValue());
}
}
Collections.sort(resources, ResourceNameComparator.INSTANCE);
return resources;
}
/**
* Method description
*
*
* @return
*/
protected List<String> getScriptResources()
{
List<String> resources = new ArrayList<>();
Collection<PluginWrapper> wrappers = pluginLoader.getInstalledPlugins();
if (wrappers != null)
{
for (PluginWrapper plugin : wrappers)
{
processPlugin(resources, plugin.getPlugin());
}
}
// fix order of script resources, see https://goo.gl/ok03l4
Collections.sort(resources);
return resources;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param resources
* @param plugin
*/
private void processPlugin(List<String> resources, Plugin plugin)
{
PluginResources pluginResources = plugin.getResources();
if (pluginResources != null)
{
Set<String> scriptResources = pluginResources.getScriptResources();
if (scriptResources != null)
{
resources.addAll(scriptResources);
}
}
}
//~--- inner classes --------------------------------------------------------
/**
* Class description
*
*
* @version Enter version here..., 12/02/03
* @author Enter your name here...
*/
protected static class ResourceKey
{
/**
* Constructs ...
*
*
* @param name
* @param type
*/
public ResourceKey(String name, ResourceType type)
{
this.name = name;
this.type = type;
}
//~--- methods ------------------------------------------------------------
/**
* Method description
*
*
* @param obj
*
* @return
*/
@Override
public boolean equals(Object obj)
{
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final ResourceKey other = (ResourceKey) obj;
if ((this.name == null)
? (other.name != null)
: !this.name.equals(other.name))
{
return false;
}
return this.type == other.type;
}
/**
* Method description
*
*
* @return
*/
@Override
public int hashCode()
{
int hash = 7;
hash = 53 * hash + ((this.name != null)
? this.name.hashCode()
: 0);
hash = 53 * hash + ((this.type != null)
? this.type.hashCode()
: 0);
return hash;
}
//~--- get methods --------------------------------------------------------
/**
* Method description
*
*
* @return
*/
public String getName()
{
return name;
}
/**
* Method description
*
*
* @return
*/
public ResourceType getType()
{
return type;
}
//~--- fields -------------------------------------------------------------
/** Field description */
private final String name;
/** Field description */
private final ResourceType type;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
protected PluginLoader pluginLoader;
/** Field description */
protected Set<ResourceHandler> resourceHandlers;
/** Field description */
protected Map<ResourceKey, Resource> resourceMap = new HashMap<>();
/** Field description */
protected ServletContext servletContext;
}

View File

@@ -1,165 +0,0 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.resources;
//~--- non-JDK imports --------------------------------------------------------
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.util.HttpUtil;
import sonia.scm.util.IOUtil;
//~--- JDK imports ------------------------------------------------------------
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 */
private static final long serialVersionUID = -1774434741744054387L;
/**
* the logger for AbstractResourceServlet
*/
private static final Logger logger =
LoggerFactory.getLogger(AbstractResourceServlet.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
* @param resourceManager
*/
public AbstractResourceServlet(ResourceManager resourceManager)
{
this.resourceManager = resourceManager;
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
protected abstract ResourceType getType();
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param request
* @param response
*
* @throws IOException
* @throws ServletException
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String uri = HttpUtil.getStrippedURI(request);
ResourceType type = getType();
String nameSeparator = HttpUtil.SEPARATOR_PATH.concat(
type.getExtension()).concat(
HttpUtil.SEPARATOR_PATH);
int index = uri.indexOf(nameSeparator);
if (index > 0)
{
String name = uri.substring(index + nameSeparator.length());
Resource resource = resourceManager.getResource(type, name);
if (resource != null)
{
printResource(response, resource);
}
else
{
if (logger.isWarnEnabled())
{
logger.warn("no resource with type {} and name {} found", type, name);
}
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
}
else
{
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
}
/**
* Method description
*
*
* @param response
* @param resource
*
* @throws IOException
*/
private void printResource(HttpServletResponse response, Resource resource)
throws IOException
{
response.setContentType(resource.getType().getContentType());
try (OutputStream output = response.getOutputStream())
{
resource.copyTo(output);
}
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private final ResourceManager resourceManager;
}

View File

@@ -1,135 +0,0 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.resources;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.plugin.PluginLoader;
import sonia.scm.util.ChecksumUtil;
//~--- JDK imports ------------------------------------------------------------
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
/**
*
* @author Sebastian Sdorra
*/
public final class DefaultResource extends AbstractResource
{
/**
* Constructs ...
*
*
* @param pluginLoader
* @param resources
* @param resourceHandlers
* @param type
*
* @throws IOException
*/
public DefaultResource(PluginLoader pluginLoader, List<String> resources,
List<ResourceHandler> resourceHandlers, ResourceType type)
throws IOException
{
super(pluginLoader, resources, resourceHandlers);
this.type = type;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream())
{
appendResources(baos);
this.content = baos.toByteArray();
this.name = ChecksumUtil.createChecksum(this.content).concat(".").concat(
type.getExtension());
}
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param output
*
* @throws IOException
*/
@Override
public void copyTo(OutputStream output) throws IOException
{
output.write(content);
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
public String getName()
{
return name;
}
/**
* Method description
*
*
* @return
*/
@Override
public ResourceType getType()
{
return type;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private final byte[] content;
/** Field description */
private final String name;
/** Field description */
private final ResourceType type;
}

View File

@@ -1,111 +0,0 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.resources;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.collect.Lists;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.plugin.PluginLoader;
//~--- JDK imports ------------------------------------------------------------
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
*
* @author Sebastian Sdorra
*/
@Singleton
public class DefaultResourceManager extends AbstractResourceManager
{
/**
* the logger for DefaultResourceManager
*/
private static final Logger logger =
LoggerFactory.getLogger(DefaultResourceManager.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
* @param pluginLoader
* @param resourceHandlers
*/
@Inject
public DefaultResourceManager(PluginLoader pluginLoader,
Set<ResourceHandler> resourceHandlers)
{
super(pluginLoader, resourceHandlers);
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param resourceMap
*/
@Override
protected void collectResources(Map<ResourceKey, Resource> resourceMap)
{
List<String> resources = getScriptResources();
try
{
Resource resource = new DefaultResource(pluginLoader, resources,
Lists.newArrayList(resourceHandlers),
ResourceType.SCRIPT);
resourceMap.put(new ResourceKey(resource.getName(), ResourceType.SCRIPT),
resource);
}
catch (IOException ex)
{
logger.error("could not collect resources", ex);
}
}
}

View File

@@ -1,135 +0,0 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.resources;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.plugin.PluginLoader;
import sonia.scm.util.HttpUtil;
//~--- JDK imports ------------------------------------------------------------
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
/**
*
* @author Sebastian Sdorra
*/
public final class DevelopmentResource extends AbstractResource
{
/**
* Constructs ...
*
*
* @param pluginLoader
* @param resources
* @param resourceHandlers
* @param name
* @param type
*/
public DevelopmentResource(PluginLoader pluginLoader, List<String> resources,
List<ResourceHandler> resourceHandlers, String name, ResourceType type)
{
super(pluginLoader, resources, resourceHandlers);
this.type = type;
if (name.startsWith(HttpUtil.SEPARATOR_PATH))
{
name = name.substring(1);
}
String ext = ".".concat(type.getExtension());
if (!name.endsWith(ext))
{
name = name.concat(ext);
}
this.name = name;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param output
*
* @throws IOException
*/
@Override
public void copyTo(OutputStream output) throws IOException
{
appendResources(output);
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
public String getName()
{
return name;
}
/**
* Method description
*
*
* @return
*/
@Override
public ResourceType getType()
{
return type;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private final String name;
/** Field description */
private final ResourceType type;
}

View File

@@ -1,117 +0,0 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.resources;
//~--- non-JDK imports --------------------------------------------------------
import com.google.inject.Inject;
import com.google.inject.Singleton;
import sonia.scm.plugin.PluginLoader;
//~--- JDK imports ------------------------------------------------------------
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
*
* @author Sebastian Sdorra
*/
@Singleton
public class DevelopmentResourceManager extends AbstractResourceManager
{
/** Field description */
public static final String PREFIX_HANDLER = "handler/";
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
* @param pluginLoader
* @param resourceHandlers
*/
@Inject
public DevelopmentResourceManager(PluginLoader pluginLoader,
Set<ResourceHandler> resourceHandlers)
{
super(pluginLoader, resourceHandlers);
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param resourceMap
*/
@Override
@SuppressWarnings("unchecked")
protected void collectResources(Map<ResourceKey, Resource> resourceMap)
{
List<String> scripts = getScriptResources();
for (String script : scripts)
{
Resource resource = new DevelopmentResource(pluginLoader,
Arrays.asList(script), Collections.EMPTY_LIST,
script, ResourceType.SCRIPT);
resourceMap.put(new ResourceKey(resource.getName(), ResourceType.SCRIPT),
resource);
}
for (ResourceHandler handler : resourceHandlers)
{
String name = handler.getName();
if (name.startsWith("/"))
{
name = name.substring(1);
}
name = PREFIX_HANDLER.concat(name);
resourceMap.put(new ResourceKey(name, ResourceType.SCRIPT),
new DevelopmentResource(pluginLoader, Collections.EMPTY_LIST,
Arrays.asList(handler), name, ResourceType.SCRIPT));
}
}
}

View File

@@ -1,78 +0,0 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.resources;
//~--- non-JDK imports --------------------------------------------------------
import com.google.inject.Inject;
import com.google.inject.Singleton;
/**
*
* @author Sebastian Sdorra
*/
@Singleton
public class ScriptResourceServlet extends AbstractResourceServlet
{
/** Field description */
private static final long serialVersionUID = 1279211769033477225L;
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
* @param resourceManager
*/
@Inject
public ScriptResourceServlet(ResourceManager resourceManager)
{
super(resourceManager);
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
protected ResourceType getType()
{
return ResourceType.SCRIPT;
}
}

View File

@@ -31,20 +31,19 @@
package sonia.scm.security;
import com.google.common.annotations.VisibleForTesting;
import java.util.Date;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.config.ScmConfiguration;
import sonia.scm.util.HttpUtil;
import sonia.scm.util.Util;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.config.ScmConfiguration;
import sonia.scm.util.HttpUtil;
import sonia.scm.util.Util;
import java.util.Date;
import java.util.concurrent.TimeUnit;
/**
* Generates cookies and invalidates access token cookies.
@@ -81,7 +80,7 @@ public final class AccessTokenCookieIssuer {
public void authenticate(HttpServletRequest request, HttpServletResponse response, AccessToken accessToken) {
LOG.trace("create and attach cookie for access token {}", accessToken.getId());
Cookie c = new Cookie(HttpUtil.COOKIE_BEARER_AUTHENTICATION, accessToken.compact());
c.setPath(request.getContextPath());
c.setPath(contextPath(request));
c.setMaxAge(getMaxAge(accessToken));
c.setHttpOnly(isHttpOnly());
c.setSecure(isSecure(request));
@@ -100,7 +99,7 @@ public final class AccessTokenCookieIssuer {
LOG.trace("invalidates access token cookie");
Cookie c = new Cookie(HttpUtil.COOKIE_BEARER_AUTHENTICATION, Util.EMPTY_STRING);
c.setPath(request.getContextPath());
c.setPath(contextPath(request));
c.setMaxAge(0);
c.setHttpOnly(isHttpOnly());
c.setSecure(isSecure(request));
@@ -108,6 +107,15 @@ public final class AccessTokenCookieIssuer {
// attach empty cookie, that the browser can remove it
response.addCookie(c);
}
@VisibleForTesting
String contextPath(HttpServletRequest request) {
String contextPath = request.getContextPath();
if (Strings.isNullOrEmpty(contextPath)) {
return "/";
}
return contextPath;
}
private int getMaxAge(AccessToken accessToken){
long maxAgeMs = accessToken.getExpiration().getTime() - new Date().getTime();

View File

@@ -38,30 +38,23 @@ package sonia.scm.template;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.SCMContextProvider;
import sonia.scm.config.ScmConfiguration;
import sonia.scm.resources.ResourceManager;
import sonia.scm.resources.ResourceType;
import sonia.scm.util.IOUtil;
//~--- JDK imports ------------------------------------------------------------
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//~--- JDK imports ------------------------------------------------------------
/**
*
@@ -100,15 +93,12 @@ public class TemplateServlet extends HttpServlet
* @param context
* @param templateEngineFactory
* @param configuration
* @param resourceManager
*/
@Inject
public TemplateServlet(SCMContextProvider context,
TemplateEngineFactory templateEngineFactory,
ResourceManager resourceManager, ScmConfiguration configuration)
TemplateEngineFactory templateEngineFactory, ScmConfiguration configuration)
{
this.templateEngineFactory = templateEngineFactory;
this.resourceManager = resourceManager;
this.configuration = configuration;
this.version = context.getVersion();
}
@@ -123,21 +113,17 @@ public class TemplateServlet extends HttpServlet
* @param response
*
* @throws IOException
* @throws ServletException
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
{
Map<String, Object> params = new HashMap<String, Object>();
Map<String, Object> params = new HashMap<>();
String contextPath = request.getContextPath();
params.put("contextPath", contextPath);
params.put("configuration", configuration);
params.put("version", version);
params.put("scripts", resourceManager.getResources(ResourceType.SCRIPT));
Locale l = request.getLocale();
if (l == null)
@@ -242,9 +228,6 @@ public class TemplateServlet extends HttpServlet
/** Field description */
private final ScmConfiguration configuration;
/** Field description */
private final ResourceManager resourceManager;
/** Field description */
private final TemplateEngineFactory templateEngineFactory;