merge with default branch

This commit is contained in:
Sebastian Sdorra
2011-11-24 19:53:29 +01:00
55 changed files with 2705 additions and 143 deletions

View File

@@ -77,6 +77,11 @@ import sonia.scm.store.StoreFactory;
import sonia.scm.template.FreemarkerTemplateHandler;
import sonia.scm.template.TemplateHandler;
import sonia.scm.template.TemplateServlet;
import sonia.scm.url.RestJsonUrlProvider;
import sonia.scm.url.RestXmlUrlProvider;
import sonia.scm.url.UrlProvider;
import sonia.scm.url.UrlProviderFactory;
import sonia.scm.url.WebUIUrlProvider;
import sonia.scm.user.UserManager;
import sonia.scm.user.xml.XmlUserManager;
import sonia.scm.util.DebugServlet;
@@ -244,6 +249,17 @@ public class ScmServletModule extends ServletModule
// bind httpclient
bind(HttpClient.class).to(URLHttpClient.class);
// bind url provider staff
bind(UrlProvider.class).annotatedWith(
Names.named(UrlProviderFactory.TYPE_RESTAPI_JSON)).toProvider(
RestJsonUrlProvider.class);
bind(UrlProvider.class).annotatedWith(
Names.named(UrlProviderFactory.TYPE_RESTAPI_XML)).toProvider(
RestXmlUrlProvider.class);
bind(UrlProvider.class).annotatedWith(
Names.named(UrlProviderFactory.TYPE_WUI)).toProvider(
WebUIUrlProvider.class);
/*
* filter(PATTERN_PAGE,
* PATTERN_STATIC_RESOURCES).through(StaticResourceFilter.class);

View File

@@ -66,7 +66,6 @@ import sonia.scm.repository.RepositoryManager;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.util.AssertUtil;
import sonia.scm.util.HttpUtil;
import sonia.scm.util.SecurityUtil;
import sonia.scm.util.Util;
import sonia.scm.web.security.WebSecurityContext;
@@ -244,18 +243,7 @@ public class RepositoryResource
@Override
public Response get(@Context Request request, @PathParam("id") String id)
{
Response response = null;
if (SecurityUtil.isAdmin(securityContextProvider))
{
response = super.get(request, id);
}
else
{
response = Response.status(Response.Status.FORBIDDEN).build();
}
return response;
return super.get(request, id);
}
/**
@@ -410,6 +398,47 @@ public class RepositoryResource
return response;
}
/**
* Returns a repository.<br />
* <br />
* Status codes:
* <ul>
* <li>200 get successful</li>
* <li>404 not found,
* no repository with the specified type and name available</li>
* <li>500 internal server error</li>
* </ul>
*
* @param request the current request
* @param type the type of the repository
* @param name the name of the repository
*
* @return the {@link Repository} with the specified type and name
*/
@GET
@Path("{type}/{name}")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@TypeHint(Repository.class)
public Response getByTypeAndName(@Context Request request,
@PathParam("type") String type,
@PathParam("name") String name)
{
Response response = null;
Repository repository = repositoryManager.get(type, name);
if (repository != null)
{
prepareForReturn(repository);
response = Response.ok(repository).build();
}
else
{
response = Response.status(Response.Status.NOT_FOUND).build();
}
return response;
}
/**
* Returns a list of {@link Changeset} for the given repository.<br />
* <br />
@@ -570,7 +599,7 @@ public class RepositoryResource
@GET
@Path("{id}/diff")
@TypeHint(DiffStreamingOutput.class)
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getDiff(@PathParam("id") String id,
@QueryParam("revision") String revision,
@QueryParam("path") String path)

View File

@@ -83,6 +83,9 @@ public class URLHttpClient implements HttpClient
/** Field description */
public static final String HEADER_ACCEPT_ENCODING_VALUE = "gzip";
/** Field description */
public static final String HEADER_AUTHORIZATION = "Authorization";
/** Field description */
public static final String HEADER_PROXY_AUTHORIZATION = "Proxy-Authorization";
@@ -141,7 +144,8 @@ public class URLHttpClient implements HttpClient
@Override
public HttpResponse post(String url) throws IOException
{
HttpURLConnection connection = (HttpURLConnection) openConnection(url);
HttpURLConnection connection = (HttpURLConnection) openConnection(null,
url);
connection.setRequestMethod(METHOD_POST);
@@ -163,10 +167,180 @@ public class URLHttpClient implements HttpClient
public HttpResponse post(String url, Map<String, List<String>> parameters)
throws IOException
{
HttpURLConnection connection = (HttpURLConnection) openConnection(url);
HttpURLConnection connection = (HttpURLConnection) openConnection(null,
url);
connection.setRequestMethod(METHOD_POST);
appendPostParameter(connection, parameters);
return new URLHttpResponse(connection);
}
/**
* Method description
*
*
* @param request
*
* @return
*
* @throws IOException
*/
@Override
public HttpResponse post(HttpRequest request) throws IOException
{
HttpURLConnection connection = (HttpURLConnection) openConnection(request,
request.getUrl());
connection.setRequestMethod(METHOD_POST);
appendPostParameter(connection, request.getParameters());
return new URLHttpResponse(connection);
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param spec
*
* @return
*
* @throws IOException
*/
@Override
public HttpResponse get(String spec) throws IOException
{
return new URLHttpResponse(openConnection(null, spec));
}
/**
* Method description
*
*
* @param url
* @param parameters
*
* @return
*
* @throws IOException
*/
@Override
public HttpResponse get(String url, Map<String, List<String>> parameters)
throws IOException
{
url = createGetUrl(url, parameters);
return new URLHttpResponse(openConnection(null, url));
}
/**
* Method description
*
*
* @param request
*
* @return
*
* @throws IOException
*/
@Override
public HttpResponse get(HttpRequest request) throws IOException
{
String url = createGetUrl(request.getUrl(), request.getParameters());
return new URLHttpResponse(openConnection(request, url));
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param connection
* @param header
* @param username
* @param password
*/
private void appendBasicAuthHeader(HttpURLConnection connection,
String header, String username,
String password)
{
if (Util.isNotEmpty(username) || Util.isNotEmpty(password))
{
username = Util.nonNull(username);
password = Util.nonNull(password);
if (logger.isDebugEnabled())
{
logger.debug("append {} header for user {}", header, username);
}
String auth = username.concat(CREDENTIAL_SEPARATOR).concat(password);
auth = new String(Base64.encode(auth.getBytes()));
connection.addRequestProperty(header,
PREFIX_BASIC_AUTHENTICATION.concat(auth));
}
}
/**
* Method description
*
*
* @param headers
* @param connection
*/
private void appendHeaders(Map<String, List<String>> headers,
URLConnection connection)
{
if (Util.isNotEmpty(headers))
{
for (Map.Entry<String, List<String>> e : headers.entrySet())
{
String name = e.getKey();
List<String> values = e.getValue();
if (Util.isNotEmpty(name) && Util.isNotEmpty(values))
{
for (String value : values)
{
if (logger.isTraceEnabled())
{
logger.trace("append header {}:{}", name, value);
}
connection.setRequestProperty(name, value);
}
}
else if (logger.isWarnEnabled())
{
logger.warn("value of {} header is empty", name);
}
}
}
else if (logger.isTraceEnabled())
{
logger.trace("header map is emtpy");
}
}
/**
* Method description
*
*
* @param connection
* @param parameters
*
* @throws IOException
*/
private void appendPostParameter(HttpURLConnection connection,
Map<String, List<String>> parameters)
throws IOException
{
if (Util.isNotEmpty(parameters))
{
connection.setDoOutput(true);
@@ -206,26 +380,6 @@ public class URLHttpClient implements HttpClient
IOUtil.close(writer);
}
}
return new URLHttpResponse(connection);
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param spec
*
* @return
*
* @throws IOException
*/
@Override
public HttpResponse get(String spec) throws IOException
{
return new URLHttpResponse(openConnection(spec));
}
/**
@@ -236,12 +390,8 @@ public class URLHttpClient implements HttpClient
* @param parameters
*
* @return
*
* @throws IOException
*/
@Override
public HttpResponse get(String url, Map<String, List<String>> parameters)
throws IOException
private String createGetUrl(String url, Map<String, List<String>> parameters)
{
if (Util.isNotEmpty(parameters))
{
@@ -275,11 +425,9 @@ public class URLHttpClient implements HttpClient
url = ub.toString();
}
return new URLHttpResponse(openConnection(url));
return url;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
@@ -306,30 +454,36 @@ public class URLHttpClient implements HttpClient
* Method description
*
*
*
* @param request
* @param spec
*
* @return
*
* @throws IOException
*/
private URLConnection openConnection(String spec) throws IOException
private HttpURLConnection openConnection(HttpRequest request, String spec)
throws IOException
{
return openConnection(new URL(spec));
return openConnection(request, new URL(spec));
}
/**
* Method description
*
*
*
* @param request
* @param url
*
* @return
*
* @throws IOException
*/
private URLConnection openConnection(URL url) throws IOException
private HttpURLConnection openConnection(HttpRequest request, URL url)
throws IOException
{
URLConnection connection = null;
HttpURLConnection connection = null;
if (configuration.isEnableProxy())
{
@@ -345,7 +499,9 @@ public class URLHttpClient implements HttpClient
new InetSocketAddress(configuration.getProxyServer(),
configuration.getProxyPort());
connection = url.openConnection(new Proxy(Proxy.Type.HTTP, address));
connection =
(HttpURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP,
address));
}
else
{
@@ -354,11 +510,25 @@ public class URLHttpClient implements HttpClient
logger.debug("fetch '{}'", url.toExternalForm());
}
connection = url.openConnection();
connection = (HttpURLConnection) url.openConnection();
}
connection.setReadTimeout(TIMEOUT_RAED);
connection.setConnectTimeout(TIMEOUT_CONNECTION);
if (request != null)
{
Map<String, List<String>> headers = request.getHeaders();
appendHeaders(headers, connection);
String username = request.getUsername();
String password = request.getPassword();
appendBasicAuthHeader(connection, HEADER_AUTHORIZATION, username,
password);
}
connection.setRequestProperty(HEADER_ACCEPT_ENCODING,
HEADER_ACCEPT_ENCODING_VALUE);
connection.setRequestProperty(
@@ -367,21 +537,8 @@ public class URLHttpClient implements HttpClient
String username = configuration.getProxyUser();
String password = configuration.getProxyPassword();
if (Util.isNotEmpty(username) || Util.isNotEmpty(password))
{
if (logger.isDebugEnabled())
{
logger.debug("enable proxy authentication for user '{}'",
Util.nonNull(username));
}
String auth = Util.nonNull(username).concat(CREDENTIAL_SEPARATOR).concat(
Util.nonNull(password));
auth = PREFIX_BASIC_AUTHENTICATION.concat(
new String(Base64.encode(auth.getBytes())));
connection.setRequestProperty(HEADER_PROXY_AUTHORIZATION, auth);
}
appendBasicAuthHeader(connection, HEADER_PROXY_AUTHORIZATION, username,
password);
return connection;
}

View File

@@ -0,0 +1,81 @@
/**
* 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.url;
//~--- non-JDK imports --------------------------------------------------------
import com.google.inject.Inject;
import com.google.inject.Provider;
import sonia.scm.config.ScmConfiguration;
/**
*
* @author Sebastian Sdorra
*/
public class RestJsonUrlProvider implements Provider<UrlProvider>
{
/**
* Constructs ...
*
*
* @param configuration
*/
@Inject
public RestJsonUrlProvider(ScmConfiguration configuration)
{
this.configuration = configuration;
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
public UrlProvider get()
{
return UrlProviderFactory.createUrlProvider(configuration.getBaseUrl(),
UrlProviderFactory.TYPE_RESTAPI_JSON);
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private ScmConfiguration configuration;
}

View File

@@ -0,0 +1,81 @@
/**
* 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.url;
//~--- non-JDK imports --------------------------------------------------------
import com.google.inject.Inject;
import com.google.inject.Provider;
import sonia.scm.config.ScmConfiguration;
/**
*
* @author Sebastian Sdorra
*/
public class RestXmlUrlProvider implements Provider<UrlProvider>
{
/**
* Constructs ...
*
*
* @param configuration
*/
@Inject
public RestXmlUrlProvider(ScmConfiguration configuration)
{
this.configuration = configuration;
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
public UrlProvider get()
{
return UrlProviderFactory.createUrlProvider(configuration.getBaseUrl(),
UrlProviderFactory.TYPE_RESTAPI_XML);
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private ScmConfiguration configuration;
}

View File

@@ -0,0 +1,81 @@
/**
* 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.url;
//~--- non-JDK imports --------------------------------------------------------
import com.google.inject.Inject;
import com.google.inject.Provider;
import sonia.scm.config.ScmConfiguration;
/**
*
* @author Sebastian Sdorra
*/
public class WebUIUrlProvider implements Provider<UrlProvider>
{
/**
* Constructs ...
*
*
* @param configuration
*/
@Inject
public WebUIUrlProvider(ScmConfiguration configuration)
{
this.configuration = configuration;
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
public UrlProvider get()
{
return UrlProviderFactory.createUrlProvider(configuration.getBaseUrl(),
UrlProviderFactory.TYPE_WUI);
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private ScmConfiguration configuration;
}