es.carebear.rightmanagement.client.group.GetGroupListOfUser.java Source code

Java tutorial

Introduction

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

import com.google.gson.Gson;
import es.carebear.rightmanagement.client.exceptions.BadRequestException;
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.group.Group;
import es.carebear.rightmanagement.core.misc.StringListContainer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.HttpStatus;

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

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

    public ArrayList<Group> getGroups(String authName, String userName) throws IOException, UnauthorizedException,
            BadRequestException, UnknownResponseException, ClassNotFoundException {
        PostMethod postMethod = new PostMethod(baseUri + "/client/groups/all/" + userName);
        postMethod.addParameter("authName", authName);
        int responseCode = httpClient.executeMethod(postMethod);

        switch (responseCode) {
        case HttpStatus.SC_ACCEPTED:
            String response = XMLHelper.fromStreamToXML(postMethod.getResponseBodyAsStream());

            StringListContainer lc = XMLHelper.fromXML(response, StringListContainer.class);
            List<Group> ls = new ArrayList<>();
            lc.getContainer().stream().forEach(obj -> {
                Gson gson = new Gson();
                ls.add(gson.fromJson(obj, Group.class));
            });
            return (ArrayList<Group>) ls;
        case HttpStatus.SC_FORBIDDEN:
            throw new UnauthorizedException();
        case HttpStatus.SC_BAD_REQUEST:
            throw new BadRequestException();
        default:
            throw new UnknownResponseException((new Integer(responseCode)).toString());
        }
    }

    public String getHighestGroup(String authName, String userName) throws UnauthorizedException,
            BadRequestException, UnknownResponseException, ClassNotFoundException, IOException {
        ArrayList<Group> al = getGroups(authName, userName);
        Group tempGroup = new Group(Integer.MAX_VALUE, "");

        for (Group g : al) {
            if (g.getId() < tempGroup.getId()) {
                tempGroup = g;
            }
        }
        return tempGroup.getName();
    }
}