http://jakarta.apache.org/slide/howto-create-users.html http://jakarta.apache.org/slide/howto-acl.html
Have a look at the projector code. It shows how to do it.
import java.util.List; import java.util.Iterator; import java.util.Vector; import java.util.Enumeration; import java.io.InputStream;
import org.apache.commons.httpclient.HttpConnection; import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.HttpState; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.protocol.Protocol; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.methods.GetMethod; // import org.apache.commons.i18n.LocalizedError;
import org.apache.webdav.lib.Property; import org.apache.webdav.lib.PropertyName; import org.apache.webdav.lib.methods.PropPatchMethod; import org.apache.webdav.lib.methods.PropFindMethod;
import org.apache.slide.projector.value.URIValue; import org.apache.slide.projector.value.XMLValue; import org.apache.slide.projector.value.ElementValue;
import org.jdom.Element; import org.jdom.output.Format; import org.jdom.input.DOMBuilder; import org.jdom.output.XMLOutputter;
import java.io.IOException;
import org.apache.webdav.lib.methods.MkcolMethod;
/**
* @author paulh
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class SlideUser {
private Credentials credentials = null;
private String host = null;
private int port = 0;
private static final String domain = "/slide";
private static final String users = "/users/";
private static final String SLIDE_NAMESPACE = "http://jakarta.apache.org/slide/";
private static final String DAV_NAMESPACE = "DAV:";
// Exceptions
public class NotFoundException extends Exception {
public NotFoundException(String s) {
super(s);
}
} public class AccessDeniedException extends Exception {
public AccessDeniedException(String s) {
super(s);
}
} public class WebdavException extends Exception {
public WebdavException(String s) {
super(s);
}
}public SlideUser(String username, String password, String host, int port) {
this.credentials = new UsernamePasswordCredentials(username, password);
this.port = port;
this.host = host;
}
public void createUser(String username, String password) throws Exception {
URIValue userUri = new URIValue("/users/" + username);
MkcolMethod mkcolMethod = new MkcolMethod("/slide" + userUri);
mkcolMethod.setDoAuthentication(true);
HttpState httpState = new HttpState(); httpState.setCredentials(null, host, this.credentials);
int state;
try {
state = mkcolMethod.execute(httpState, new HttpConnection(host,
port, Protocol.getProtocol("http")));
if (state == HttpStatus.SC_CREATED) {
System.out.println("Created");
changePassword(userUri, null, password);
} else if (state == HttpStatus.SC_FORBIDDEN) {
System.err.println("Forbidden");
}
} catch (IOException e) {
System.err.println(e.toString());
}
} public void changePassword(URIValue uri, String oldPassword,
String newPassword) throws Exception {
System.out.println("SlideUser.changePassword : " + uri);
setProperty(uri, SLIDE_NAMESPACE, "password", newPassword,
this.credentials);
}public void addRole(URIValue user, URIValue role) throws Exception {
XMLValue membersElement = getPropertyAsXMLValue(role, DAV_NAMESPACE,
"group-member-set", credentials);
// remove member from role to avoid double membership
removeRole(membersElement.getRootElement().getChildren(), domain + user);
Element newUserElement = new Element("href", "DAV:");
newUserElement.addContent(user.toString());
membersElement.getRootElement().addContent(newUserElement);
XMLOutputter xout = new XMLOutputter(Format.getCompactFormat());
String groupMemberSet = xout.outputString(membersElement
.getRootElement().getContent());
setProperty(role, DAV_NAMESPACE, "group-member-set", groupMemberSet,
this.credentials);
}
public void removeRole(URIValue user, URIValue role, Credentials credentials)
throws Exception {
XMLValue membersElement = getPropertyAsXMLValue(role, DAV_NAMESPACE,
"group-member-set", credentials);
removeRole(membersElement.getRootElement().getChildren(), domain + user);
XMLOutputter xout = new XMLOutputter(Format.getCompactFormat());
String groupMemberSet = xout.outputString(membersElement
.getRootElement().getContent());
setProperty(role, DAV_NAMESPACE, "group-member-set", groupMemberSet,
credentials);
}
private void removeRole(List members, String user) {
for (Iterator i = members.iterator(); i.hasNext();) {
Element memberElement = (Element) i.next();
String uri = memberElement.getTextTrim();
if (uri.equals(user)) {
i.remove();
break;
}
}
}public void setProperty(URIValue uri, String namespace, String name,
String value, Credentials credentials) throws Exception {
System.out.println("SlideUser.setProperty : " + uri);
PropPatchMethod proppatchMethod = new PropPatchMethod(domain + uri);
proppatchMethod.setDoAuthentication(true);
HttpState httpState = new HttpState();
httpState.setCredentials(null, host, credentials);
proppatchMethod.addPropertyToSet(name, value, "S", namespace);
try {
int state = proppatchMethod.execute(httpState, new HttpConnection(
host, port, Protocol.getProtocol("http")));
if (state != HttpStatus.SC_MULTI_STATUS) {
System.err.println("Error occurred");
throw new IOException();
}
} catch (Exception e) {
System.err.println("Something went wrong : " + e.toString());
throw e;
}
}
public XMLValue getPropertyAsXMLValue(URIValue uri, String namespace,
String name, Credentials credentials) throws Exception {
Property property = getProperty(uri, namespace, name, credentials);
if (property == null)
return null;
DOMBuilder builder = new DOMBuilder();
Element element = builder.build(property.getElement());
return new ElementValue(element);
}private Property getProperty(URIValue uri, String namespace, String name,
Credentials credentials) throws Exception {
String url = domain + uri;
Vector props = new Vector();
props.add(new PropertyName(namespace, name));
PropFindMethod propfindMethod = new PropFindMethod(url, 0, props
.elements());
propfindMethod.setDoAuthentication(true);
HttpState httpState = new HttpState();
httpState.setCredentials(null, host, credentials);
try {
int state = propfindMethod.execute(httpState, new HttpConnection(
host, port, Protocol.getProtocol("http")));
if (state != HttpStatus.SC_MULTI_STATUS) {
throw new Exception();
}
} catch (IOException e) {
throw new Exception("repositoryException");
}
Enumeration propertyEnumeration = propfindMethod
.getResponseProperties(url);
if (propertyEnumeration.hasMoreElements()) {
return (Property) propertyEnumeration.nextElement();
}
return null;
}
public InputStream getResource(URIValue uri) throws Exception {
String url = domain + uri;
GetMethod getMethod = new GetMethod(url);
getMethod.setDoAuthentication(true);
HttpState httpState = new HttpState();
httpState.setCredentials(null, host, this.credentials);
try {
int state = getMethod.execute(httpState, new HttpConnection(host,
port, Protocol.getProtocol("http")));
if (state == HttpStatus.SC_NOT_FOUND) {
System.err.println("SlideUser.getResource : SC_NOT_FOUND");
return null;
} else if (state == HttpStatus.SC_FORBIDDEN) {
System.err.println("SlideUser.getResource : SC_FORBIDDEN");
return null;
} else if (state == HttpStatus.SC_OK) {
System.out.println("SlideUser.getResource : SC_OK");
InputStream stream = getMethod.getResponseBodyAsStream();
return stream;
} else {
System.err
.println("SlideUser.getResource : OOPS, should never get here");
return null;
}
} catch (IOException e) {
// throw new RepositoryException ( "repositoryException" );
throw e;
}
}
public static void main(String[] args) throws Exception { URIValue userUri = new URIValue("/users/" + "test");
SlideUser test = new SlideUser("root", "root", "127.0.0.1", 8080);
if (test.getResource(userUri) == null) {
test.createUser("test", "test");
test.addRole(userUri, new URIValue("/roles/user")); } else {
test.changePassword(userUri, null, "tester");}
} }
Ethem YUKSEL wrote:
Hi to all, do i have a chance to create a user at the run time? i don't want to add the user to Domain.xml. instead when a user logins into my application i want to create a slide user for it. any help will be appreciated.
kid regards... ethem yuksel
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
.
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
