dtu.ds.warnme.app.ws.client.restful.utils.RestUtils.java Source code

Java tutorial

Introduction

Here is the source code for dtu.ds.warnme.app.ws.client.restful.utils.RestUtils.java

Source

/**
 * Project:   warnme-app
 * File:      RestUtils.java
 * License: 
 *            This file is licensed under GNU General Public License version 3
 *            http://www.gnu.org/licenses/gpl-3.0.txt
 *
 * Copyright: Bartosz Cichecki [ cichecki.bartosz@gmail.com ]
 * Date:      24-12-2012
 */

package dtu.ds.warnme.app.ws.client.restful.utils;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;

import android.util.Log;
import dtu.ds.warnme.app.application.Prefs;
import dtu.ds.warnme.app.utils.SecurityUtils;
import dtu.ds.warnme.app.ws.client.https.utils.HttpConstants;
import dtu.ds.warnme.app.ws.client.restful.RestClient;

/**
 * @author Bartosz Cichecki
 * 
 */
public class RestUtils {

    private static final String TAG = "RestUtils";

    public static void decorareHeaderWithContentType(List<Header> headers, String contentType) {
        headers.add(new BasicHeader(HttpConstants.CONTENT_TYPE, contentType));
    }

    public static void decorareHeaderWithMD5(List<Header> headers, String content) {
        if (Prefs.isCheckOutgoingMD5()) {
            headers.add(new BasicHeader(HttpConstants.CONTENT_MD5, SecurityUtils.hashMD5Base64(content)));
        }
    }

    public static String getAbsoluteAddress(RestClient rc, String... resourceSuffixes) {
        StringBuilder absoluteAddress = new StringBuilder();
        absoluteAddress.append(HttpConstants.PROTOCOL_HTTPS).append(rc.getHost()).append(HttpConstants.COLON)
                .append(rc.getPort()).append(rc.getWsContextPath());
        for (String resourceSuffix : resourceSuffixes) {
            if (!resourceSuffix.startsWith(HttpConstants.PATH_SEPARATOR)) {
                absoluteAddress.append(HttpConstants.PATH_SEPARATOR);
            }
            if (resourceSuffix.endsWith(HttpConstants.PATH_SEPARATOR)) {
                resourceSuffix = StringUtils.substringBeforeLast(resourceSuffix, HttpConstants.PATH_SEPARATOR);
            }
            absoluteAddress.append(resourceSuffix);
        }

        Log.d(TAG, "Constructed address: " + absoluteAddress.toString());

        return absoluteAddress.toString();
    }

    public static String getCommaSeparatedString(Object[] objs) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < objs.length; i++) {
            sb.append(objs[i]);
            if (i < objs.length - 1) {
                sb.append(",");
            }
        }
        return sb.toString();
    }

    public static Header[] getHeadersAsArray(List<Header> headers) {
        return headers.toArray(new Header[headers.size()]);
    }
}