es.carebear.rightmanagement.client.admin.CreateUser.java Source code

Java tutorial

Introduction

Here is the source code for es.carebear.rightmanagement.client.admin.CreateUser.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package es.carebear.rightmanagement.client.admin;

import es.carebear.rightmanagement.client.xml.XMLHelper;
import es.carebear.rightmanagement.client.exceptions.*;
import es.carebear.rightmanagement.core.exceptions.UnauthorizedException;
import es.carebear.rightmanagement.core.user.User;
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.HttpStatus;

/**
 *
 * @author Felix
 */
public class CreateUser {
    private String baseUri;
    private HttpClient httpClient;

    public CreateUser(String baseUri) {
        this.baseUri = baseUri;
        this.httpClient = new HttpClient();
    }

    public User createUser(String authName, String userName, String password)
            throws IOException, IllegalArgumentException, InternalServerErrorException, BadRequestException,
            UnauthorizedException, UnknownResponseException {
        PostMethod postMethod = new PostMethod(baseUri + "/client/users/create/" + userName);
        postMethod.addParameter("creatorName", authName);
        postMethod.addParameter("password", password);
        int responseCode = httpClient.executeMethod(postMethod);

        switch (responseCode) {
        case HttpStatus.SC_CREATED:
            String response = postMethod.getResponseBodyAsString();
            return XMLHelper.fromXML(response, User.class);
        case HttpStatus.SC_INTERNAL_SERVER_ERROR:
            throw new InternalServerErrorException();
        case HttpStatus.SC_UNAUTHORIZED:
            throw new UnauthorizedException();
        case HttpStatus.SC_BAD_REQUEST:
            throw new BadRequestException();
        default:
            throw new UnknownResponseException((new Integer(responseCode)).toString());

        }

    }
}