org.getwheat.harvest.library.request.ParameterHelper.java Source code

Java tutorial

Introduction

Here is the source code for org.getwheat.harvest.library.request.ParameterHelper.java

Source

package org.getwheat.harvest.library.request;

import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.getwheat.harvest.library.model.RequestParameter;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * Copyright 2011 by Christopher Smith
 * 
 * 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.
 */

/**
 * This is a utility class used to format and encode HTTP parameters.
 * 
 * @author Christopher Smith
 */
public class ParameterHelper {
    private static final Logger LOG = LoggerFactory.getLogger(ParameterHelper.class);
    private static final String UPDATED_SINCE_PAT = "yyyy-MM-dd HH:mm";
    private static final String VALUE_UTF8 = "UTF-8";

    /**
     * Adds the Update Since RequestParameter to the parameters List.
     * <p />
     * The updatedSince parameter is formatted to UTC time.
     * 
     * @param updatedSince
     * @param parameters
     */
    public void addUpdatedSince(final Date updatedSince, final List<NameValuePair> parameters) {
        if (updatedSince != null) {
            final DateTimeFormatter formatter = DateTimeFormat.forPattern(UPDATED_SINCE_PAT)
                    .withZone(DateTimeZone.UTC);
            final DateTime date = new DateTime(updatedSince.getTime());
            try {
                addParameter(RequestParameter.UPDATED_SINCE, formatter.print(date), parameters);
            } catch (Exception ex) {
                LOG.warn("", ex);
            }
        }
    }

    /**
     * Adds a RequestParameter and value to the parameters List.
     * <p />
     * If the RequestParameter or value is null, nothing is added.
     * 
     * @param parameter
     * @param value
     * @param parameters
     */
    public void addParameter(final RequestParameter parameter, final String value,
            final List<NameValuePair> parameters) {
        if (parameter != null && value != null && parameters != null) {
            parameters.add(new BasicNameValuePair(parameter.getParameter(), value));
        }
    }

    /**
     * Encodes the value into a format that is suitable for HTTP transmission.
     * 
     * @param value the value to encode
     * @return the encoded value
     */
    public String encode(final String value) {
        String record = "";
        try {
            record = URLEncoder.encode(value, VALUE_UTF8);
        } catch (Exception ex) {
            LOG.warn("", ex);
        }
        return record;
    }
}