org.labkey.freezerpro.export.GetFreezersCommand.java Source code

Java tutorial

Introduction

Here is the source code for org.labkey.freezerpro.export.GetFreezersCommand.java

Source

/*
 * Copyright (c) 2014 LabKey Corporation
 *
 * 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.labkey.freezerpro.export;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.message.BasicNameValuePair;
import org.labkey.api.pipeline.PipelineJob;

import java.util.ArrayList;
import java.util.List;

/**
 * Returns the list of freezers configured in the server
 */
public class GetFreezersCommand {
    private String _url;
    private String _username;
    private String _password;
    private FreezerProExport _export;

    public GetFreezersCommand(FreezerProExport export, String url, String username, String password) {
        _export = export;
        _url = url;
        _username = username;
        _password = password;
    }

    public FreezerProCommandResonse execute(HttpClient client, PipelineJob job) {
        HttpPost post = new HttpPost(_url);

        try {
            List<NameValuePair> params = new ArrayList<NameValuePair>();

            params.add(new BasicNameValuePair("method", "freezers"));
            params.add(new BasicNameValuePair("username", _username));
            params.add(new BasicNameValuePair("password", _password));

            post.setEntity(new UrlEncodedFormEntity(params));

            ResponseHandler<String> handler = new BasicResponseHandler();

            HttpResponse response = client.execute(post);
            StatusLine status = response.getStatusLine();

            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
                return new GetFreezersResponse(_export, handler.handleResponse(response), status.getStatusCode(),
                        job);
            else
                return new GetFreezersResponse(_export, status.getReasonPhrase(), status.getStatusCode(), job);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}