es.carebear.rightmanagement.client.RestClient.java Source code

Java tutorial

Introduction

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

import es.carebear.rightmanagement.client.xml.XMLHelper;
import es.carebear.rightmanagement.core.misc.StringListContainer;
import es.carebear.rightmanagement.core.user.User;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.HttpClient;

/**
 *
 * @author Felix
 */
public abstract class RestClient {
    private static final String _serviceBaseURI = "http://localhost:8080";
    private final String serviceBaseURI;
    protected final String xml = "application/xml";
    protected final String json = "application/json";

    public RestClient() {
        this(_serviceBaseURI);
    }

    public RestClient(String serviceBaseURI) {
        this.serviceBaseURI = serviceBaseURI;
    }

    public String getServiceBaseURI() {
        return serviceBaseURI;
    }

    public String responseToString(InputStream stream) {
        if (stream == null) {
            return null;
        }
        StringBuilder result = new StringBuilder();
        try (BufferedReader br = new BufferedReader(new InputStreamReader(stream))) {
            System.out.println("HTTP response body:");
            String readLine;
            while (((readLine = br.readLine()) != null)) {
                result.append(readLine);
            }
        } catch (IOException io) {
        }

        return result.toString();
    }

    public User registerTest(String username, String password) throws IOException {

        HttpClient client = new HttpClient();

        PostMethod method = new PostMethod(getServiceBaseURI() + "/client/users/register/" + username);

        method.addParameter("password", password);

        System.out.println(method.getPath());

        int responseCode = client.executeMethod(method);

        String response = responseToString(method.getResponseBodyAsStream());

        System.out.println(response);

        return XMLHelper.fromXML(response, User.class);
    }

    public User logInTest(String username, String password) throws IOException {

        HttpClient client = new HttpClient();

        PostMethod method = new PostMethod(getServiceBaseURI() + "/client/users/login/" + username);

        method.addParameter("password", password);

        System.out.println(method.getPath());

        int responseCode = client.executeMethod(method);

        String response = responseToString(method.getResponseBodyAsStream());

        System.out.println(response);

        return XMLHelper.fromXML(response, User.class);
    }

    public void deleteTest(String authName, String target) throws IOException {
        HttpClient client = new HttpClient();

        PostMethod deleteMethod = new PostMethod(getServiceBaseURI() + "/client/users/delete/" + target);

        deleteMethod.addParameter("authName", "Admin");

        int responseCode = client.executeMethod(deleteMethod);

        System.out.println(responseCode);
    }

    public void authTest() throws IOException {
        HttpClient client = new HttpClient();

        PostMethod deleteMethod = new PostMethod(getServiceBaseURI() + "/client/users/change/allowed/" + "Paul");

        deleteMethod.addParameter("authName", "Admin");
        deleteMethod.addParameter("rigthTarget", "User");
        deleteMethod.addParameter("rightMask", "DELETE");

        int responseCode = client.executeMethod(deleteMethod);

        System.out.println(responseCode);
    }

    //neue methode
    public void getGroupsTest(String authName) throws IOException {
        HttpClient client = new HttpClient();

        PostMethod deleteMethod = new PostMethod(getServiceBaseURI() + "/client/groups/all/names");

        deleteMethod.addParameter("authName", "Admin");

        int responseCode = client.executeMethod(deleteMethod);

        String response = responseToString(deleteMethod.getResponseBodyAsStream());

        System.out.println(responseCode);

    }

    public void getAllRigthsOfGroups() throws IOException {
        HttpClient client = new HttpClient();

        PostMethod deleteMethod = new PostMethod(getServiceBaseURI() + "/client/groups/all/rights/" + "Moderator");

        deleteMethod.addParameter("authName", "Admin");

        int responseCode = client.executeMethod(deleteMethod);

        String response = responseToString(deleteMethod.getResponseBodyAsStream());

        System.out.println(responseCode);

    }

    public void getAllUsersWithAccess() throws IOException {
        HttpClient client = new HttpClient();

        PostMethod deleteMethod = new PostMethod(getServiceBaseURI() + "/client/groups/users/" + "User");

        deleteMethod.addParameter("authName", "Admin");

        int responseCode = client.executeMethod(deleteMethod);

        String response = responseToString(deleteMethod.getResponseBodyAsStream());

        System.out.println(responseCode);

        StringListContainer lc = XMLHelper.fromXML(response, StringListContainer.class);

        lc.getContainer().stream().forEach(w -> {
            System.out.println("ULUMULU" + w);
        });
    }

    public void getAllRigthsOfGroups22() throws IOException {
        HttpClient client = new HttpClient();

        PostMethod deleteMethod = new PostMethod(getServiceBaseURI() + "/client/groups/userlist/" + "User");

        deleteMethod.addParameter("authnm", "Admin");

        int responseCode = client.executeMethod(deleteMethod);

        //String response = responseToString(deleteMethod.getResponseBodyAsStream());

        System.out.println(responseCode);

        //StringListContainer lc = XMLHelper.fromXML(response, StringListContainer.class);

        //lc.getContainer().stream().forEach(w->{System.out.println(w);});
    }

    public void getOwnZeuch() throws IOException {
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(getServiceBaseURI() + "/client/groups/all/rights/" + "User");
        postMethod.addParameter("authName", "Admin");
        int responseCode = client.executeMethod(postMethod);
        System.out.println(responseCode);
    }

}