added version to ScmState

This commit is contained in:
Sebastian Sdorra
2011-02-10 16:10:51 +01:00
parent 9f69ff3c6f
commit cd92d776c1
4 changed files with 138 additions and 9 deletions

View File

@@ -29,6 +29,8 @@
*
*/
package sonia.scm;
//~--- non-JDK imports --------------------------------------------------------
@@ -39,6 +41,9 @@ import sonia.scm.util.Util;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
*
@@ -47,6 +52,9 @@ import java.io.IOException;
public class BasicContextProvider implements SCMContextProvider
{
/** Field description */
public static final String DEFAULT_VERSION = "unknown";
/** Field description */
public static final String DIRECTORY_DEFAULT = ".scm";
@@ -56,6 +64,13 @@ public class BasicContextProvider implements SCMContextProvider
/** Field description */
public static final String DIRECTORY_PROPERTY = "scm.home";
/** Field description */
public static final String MAVEN_PROPERTIES =
"/META-INF/maven/sonia.scm/scm-core/pom.properties";
/** Field description */
public static final String MAVEN_PROPERTY_VERSION = "version";
//~--- constructors ---------------------------------------------------------
/**
@@ -65,6 +80,7 @@ public class BasicContextProvider implements SCMContextProvider
public BasicContextProvider()
{
baseDirectory = findBaseDirectory();
version = loadVersion();
}
//~--- methods --------------------------------------------------------------
@@ -99,6 +115,18 @@ public class BasicContextProvider implements SCMContextProvider
return baseDirectory;
}
/**
* Method description
*
*
* @return
*/
@Override
public String getVersion()
{
return version;
}
//~--- methods --------------------------------------------------------------
/**
@@ -132,8 +160,38 @@ public class BasicContextProvider implements SCMContextProvider
return directory;
}
/**
* Method description
*
*
* @return
*/
private String loadVersion()
{
Properties properties = new Properties();
InputStream input =
BasicContextProvider.class.getResourceAsStream(MAVEN_PROPERTIES);
if (input != null)
{
try
{
properties.load(input);
}
catch (IOException ex)
{
throw new ConfigurationException(ex);
}
}
return properties.getProperty(MAVEN_PROPERTY_VERSION, DEFAULT_VERSION);
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private File baseDirectory;
/** Field description */
private String version;
}

View File

@@ -29,6 +29,8 @@
*
*/
package sonia.scm;
//~--- JDK imports ------------------------------------------------------------
@@ -58,4 +60,12 @@ public interface SCMContextProvider extends Closeable
* @return
*/
public File getBaseDirectory();
/**
* Method description
*
*
* @return
*/
public String getVersion();
}

View File

@@ -67,12 +67,16 @@ public class ScmState
*
*
*
*
* @param provider
* @param securityContext
* @param repositoryTypes
*/
public ScmState(WebSecurityContext securityContext,
public ScmState(SCMContextProvider provider,
WebSecurityContext securityContext,
Collection<Type> repositoryTypes)
{
this.version = provider.getVersion();
this.user = securityContext.getUser();
this.groups = securityContext.getGroups();
this.repositoryTypes = repositoryTypes;
@@ -113,6 +117,17 @@ public class ScmState
return user;
}
/**
* Method description
*
*
* @return
*/
public String getVersion()
{
return version;
}
/**
* Method description
*
@@ -170,6 +185,17 @@ public class ScmState
this.user = user;
}
/**
* Method description
*
*
* @param version
*/
public void setVersion(String version)
{
this.version = version;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
@@ -184,4 +210,7 @@ public class ScmState
/** Field description */
private User user;
/** Field description */
private String version;
}

View File

@@ -36,12 +36,14 @@ package sonia.scm.api.rest.resources;
//~--- non-JDK imports --------------------------------------------------------
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.SCMContext;
import sonia.scm.SCMContextProvider;
import sonia.scm.ScmState;
import sonia.scm.repository.RepositoryManager;
import sonia.scm.user.User;
@@ -66,8 +68,8 @@ import javax.ws.rs.core.Response;
*
* @author Sebastian Sdorra
*/
@Path("authentication")
@Singleton
@Path("authentication")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public class AuthenticationResource
{
@@ -76,6 +78,27 @@ public class AuthenticationResource
private static final Logger logger =
LoggerFactory.getLogger(AuthenticationResource.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
* @param contextProvider
* @param repositoryManger
* @param securityContextProvider
*/
@Inject
public AuthenticationResource(
SCMContextProvider contextProvider,
RepositoryManager repositoryManger,
Provider<WebSecurityContext> securityContextProvider)
{
this.contextProvider = contextProvider;
this.repositoryManger = repositoryManger;
this.securityContextProvider = securityContextProvider;
}
//~--- methods --------------------------------------------------------------
/**
@@ -97,12 +120,14 @@ public class AuthenticationResource
@FormParam("password") String password)
{
ScmState state = null;
WebSecurityContext securityContext = securityContextProvider.get();
User user = securityContext.authenticate(request, response, username,
password);
if ((user != null) &&!SCMContext.USER_ANONYMOUS.equals(user.getName()))
{
state = new ScmState(securityContext, repositoryManger.getTypes());
state = new ScmState(contextProvider, securityContext,
repositoryManger.getTypes());
}
else
{
@@ -126,6 +151,8 @@ public class AuthenticationResource
public Response logout(@Context HttpServletRequest request,
@Context HttpServletResponse response)
{
WebSecurityContext securityContext = securityContextProvider.get();
securityContext.logout(request, response);
Response resp = null;
@@ -133,8 +160,10 @@ public class AuthenticationResource
if (user != null)
{
resp = Response.ok(new ScmState(securityContext,
repositoryManger.getTypes())).build();
ScmState state = new ScmState(contextProvider, securityContext,
repositoryManger.getTypes());
resp = Response.ok(state).build();
}
else
{
@@ -159,6 +188,7 @@ public class AuthenticationResource
{
Response response = null;
ScmState state = null;
WebSecurityContext securityContext = securityContextProvider.get();
User user = securityContext.getUser();
if (user != null)
@@ -168,7 +198,8 @@ public class AuthenticationResource
logger.debug("return state for user {}", user.getName());
}
state = new ScmState(securityContext, repositoryManger.getTypes());
state = new ScmState(contextProvider, securityContext,
repositoryManger.getTypes());
response = Response.ok(state).build();
}
else
@@ -182,10 +213,11 @@ public class AuthenticationResource
//~--- fields ---------------------------------------------------------------
/** Field description */
@Inject
private SCMContextProvider contextProvider;
/** Field description */
private RepositoryManager repositoryManger;
/** Field description */
@Inject
private WebSecurityContext securityContext;
private Provider<WebSecurityContext> securityContextProvider;
}