added ScmState

This commit is contained in:
Sebastian Sdorra
2010-09-06 14:34:04 +02:00
parent fe65284cc5
commit 45134a2964
8 changed files with 267 additions and 28 deletions

View File

@@ -7,6 +7,11 @@
package sonia.scm.api.rest.resources;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.RepositoryType;
import sonia.scm.ScmState;
//~--- JDK imports ------------------------------------------------------------
import javax.inject.Singleton;
@@ -18,6 +23,7 @@ import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@@ -43,27 +49,25 @@ public class AuthenticationResource
* @return
*/
@POST
public Response authenticate(@Context HttpServletRequest request,
@FormParam("username") String username,
@FormParam("password") String password)
public ScmState getState(@Context HttpServletRequest request,
@FormParam("username") String username,
@FormParam("password") String password)
{
Response response = null;
ScmState state = null;
if ("hans".equals(username) && "hans123".equals(password))
{
request.getSession(true).setAttribute("auth", Boolean.TRUE);
response = Response.ok().build();
request.getSession(true).setAttribute("auth", username);
state = getState(username);
}
else
{
response = Response.status(Response.Status.UNAUTHORIZED).build();
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
}
return response;
return state;
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
@@ -73,21 +77,44 @@ public class AuthenticationResource
* @return
*/
@GET
public Response isAuthenticated(@Context HttpServletRequest request)
public ScmState getState(@Context HttpServletRequest request)
{
Response response = null;
ScmState state = null;
String username = (String) request.getSession(true).getAttribute("auth");
if (request.getSession(true).getAttribute("auth") != null)
if (username != null)
{
System.out.println( "authenticated" );
response = Response.ok().build();
state = getState(username);
}
else
{
response = Response.status(Response.Status.UNAUTHORIZED).build();
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
}
return response;
return state;
}
/**
* Method description
*
*
* @param username
*
* @return
*/
private ScmState getState(String username)
{
ScmState state = new ScmState();
state.setUsername(username);
RepositoryType[] types = new RepositoryType[] {
new RepositoryType("hg", "Mercurial"),
new RepositoryType("svn", "Subversion"),
new RepositoryType("git", "Git") };
state.setRepositoryTypes(types);
return state;
}
}