org.openscore.content.httpclient.build.ContentTypeBuilder.java Source code

Java tutorial

Introduction

Here is the source code for org.openscore.content.httpclient.build.ContentTypeBuilder.java

Source

/*******************************************************************************
* (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Apache License v2.0 which accompany this distribution.
*
* The Apache License is available at
* http://www.apache.org/licenses/LICENSE-2.0
*
*******************************************************************************/

package org.openscore.content.httpclient.build;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.ParseException;
import org.apache.http.entity.ContentType;
import org.openscore.content.httpclient.HttpClientInputs;

import java.nio.charset.UnsupportedCharsetException;

/**
 * Created with IntelliJ IDEA.
 * User: tusaa
 * Date: 8/12/14
 */
public class ContentTypeBuilder {

    private ContentType defaultContentType = ContentType.TEXT_PLAIN;
    private String contentType;
    private String requestCharacterSet;

    public ContentTypeBuilder setContentType(String contentType) {
        this.contentType = contentType;
        return this;
    }

    public ContentTypeBuilder setRequestCharacterSet(String requestCharacterSet) {
        if (!StringUtils.isEmpty(requestCharacterSet)) {
            this.requestCharacterSet = requestCharacterSet;
        }
        return this;
    }

    public ContentType buildContentType() {
        String contentType = this.contentType;
        String requestCharacterSet = this.requestCharacterSet;
        ContentType parsedContentType;
        if (StringUtils.isEmpty(contentType)) {
            parsedContentType = defaultContentType;
        } else {
            try {
                parsedContentType = ContentType.parse(contentType);
            } catch (ParseException | UnsupportedCharsetException e) {
                throw new IllegalArgumentException(
                        "Could not parse input '" + HttpClientInputs.CONTENT_TYPE + "'. " + e.getMessage(), e);
            }
        }
        //do not override contentType provide by user
        if (!StringUtils.isEmpty(requestCharacterSet)) {
            try {
                parsedContentType = parsedContentType.withCharset(requestCharacterSet);
            } catch (UnsupportedCharsetException e) {
                throw new IllegalArgumentException(
                        "Could not parse input '" + HttpClientInputs.REQUEST_CHARACTER_SET + "'. " + e.getMessage(),
                        e);
            }
        }
        return parsedContentType;
    }
}