es.carebear.rightmanagement.client.user.GetUserAttributes.java Source code

Java tutorial

Introduction

Here is the source code for es.carebear.rightmanagement.client.user.GetUserAttributes.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.user;

import es.carebear.rightmanagement.client.exceptions.BadRequestException;
import es.carebear.rightmanagement.client.exceptions.InternalServerErrorException;
import es.carebear.rightmanagement.client.exceptions.UnknownResponseException;
import es.carebear.rightmanagement.client.xml.XMLHelper;
import es.carebear.rightmanagement.core.exceptions.UnauthorizedException;
import es.carebear.rightmanagement.core.misc.StringListContainer;
import es.carebear.rightmanagement.core.user.AttributeContainer;
import es.carebear.rightmanagement.core.user.User;
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.omg.CORBA.UNKNOWN;

/**
 *
 * @author hannes
 */
public class GetUserAttributes {
    private String baseUri;
    private HttpClient httpClient;

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

    public String[] getAttributes(String authName, String target) throws BadRequestException,
            InternalServerErrorException, IOException, UnauthorizedException, UnknownResponseException {
        PostMethod postMethod = new PostMethod(baseUri + "/client/users/" + target);
        postMethod.addParameter("authName", authName);
        int responseCode = httpClient.executeMethod(postMethod);
        System.err.println(responseCode);

        switch (responseCode) {
        case HttpStatus.SC_ACCEPTED:
            String response = XMLHelper.fromStreamToXML(postMethod.getResponseBodyAsStream());
            AttributeContainer lc = XMLHelper.fromXML(response, AttributeContainer.class);
            return lc.toArray();
        case HttpStatus.SC_BAD_REQUEST:
            throw new BadRequestException();
        case HttpStatus.SC_UNAUTHORIZED:
            throw new UnauthorizedException();
        default:
            throw new UnknownResponseException((new Integer(responseCode)).toString());
        }
    }
}