org.perfcake.reporting.destination.Client.java Source code

Java tutorial

Introduction

Here is the source code for org.perfcake.reporting.destination.Client.java

Source

/*
 * Copyright 2017 PerfCake Community.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.perfcake.reporting.destination;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.codec.binary.Base64;

/**
 *
 * @author totoro
 */
public class Client {

    private static final String REST_MATCH = "http://%s/";
    private static final String CONTENT_TYPE_XML = "text/xml";
    private static final String REST_TEST_GET = "rest/test/uid/%s";
    private static final String REST_EXEC_CREATE = "rest/testExecution/create";
    private static final String REST_EXEC_ADD_VALUE = "rest/testExecution/addValue";

    private String username;
    private String password;
    private String repositoryUrl;
    private String authorizationHeader;

    private URL urlCreate;
    private URL urlAdd;

    public Client(String username, String password, String repositoryUrl) throws MalformedURLException {
        this.username = username;
        this.password = password;
        this.repositoryUrl = repositoryUrl;

        urlCreate = new URL(String.format(REST_MATCH, repositoryUrl) + REST_EXEC_CREATE);
        urlAdd = new URL(String.format(REST_MATCH, repositoryUrl) + REST_EXEC_ADD_VALUE);

        authorizationHeader = Base64.encodeBase64String((username + ":" + password).getBytes()).trim();
    }

    public long createTestExecution(TestExec te) throws Exception {
        Long id = null;
        HttpURLConnection conn = (HttpURLConnection) urlCreate.openConnection();

        conn.setRequestMethod("POST");
        conn.setRequestProperty("Authorization", "Basic " + authorizationHeader);
        conn.setRequestProperty("Content-Type", "text/xml");

        conn.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());

        wr.writeBytes(te.toXml());
        wr.flush();
        wr.close();

        int responseCode = conn.getResponseCode();

        if (responseCode == 201) {
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

            String response = in.readLine();

            id = Long.parseLong(response);
            te.setId(id);

            in.close();
        } else {
            //TODO ERROR
        }

        conn.disconnect();

        return id;
    }

    public void addValue(TestExec te) throws Exception {
        HttpURLConnection conn = (HttpURLConnection) urlAdd.openConnection();

        conn.setRequestMethod("POST");
        conn.setRequestProperty("Authorization", "Basic " + authorizationHeader);
        conn.setRequestProperty("Content-Type", "text/xml");

        conn.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
        wr.writeBytes(te.toXml());
        wr.flush();
        wr.close();

        int responseCode = conn.getResponseCode();

        if (responseCode == 201) {
        } else {
            //TODO ERROR
        }

        conn.disconnect();
    }

    //TODO complete
    public String getTest(String testUid) throws Exception {

        HttpURLConnection conn = (HttpURLConnection) urlAdd.openConnection();

        conn.setRequestMethod("GET");
        conn.setRequestProperty("Authorization", "Basic " + authorizationHeader);

        int responseCode = conn.getResponseCode();
        if (responseCode == 200) {
        }

        conn.disconnect();

        return null;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRepositoryUrl() {
        return repositoryUrl;
    }

    public void setRepositoryUrl(String repositoryUrl) {
        this.repositoryUrl = repositoryUrl;
    }
}