/* 
 * HttpUtility.java 
 * 
 * Version: 
 *     $Id$ 
 * 
 * Revisions: 
 *     $Log$ 
 */
package edu.tamu.framework.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * Http Utility
 * 
 * @author <a href="mailto:jmicah@library.tamu.edu">Micah Cooper</a>
 * @author <a href="mailto:jcreel@library.tamu.edu">James Creel</a>
 * @author <a href="mailto:huff@library.tamu.edu">Jeremy Huff</a>
 * @author <a href="mailto:jsavell@library.tamu.edu">Jason Savell</a>
 * @author <a href="mailto:wwelling@library.tamu.edu">William Welling</a>
 *
 */
@Service
public class HttpUtility {

    @Value("${app.http.timeout}")
    private int DEFAULT_TIMEOUT;

    public String makeHttpRequest(String urlString, String method) throws IOException {
        return makeHttpRequest(urlString, method, Optional.empty(), DEFAULT_TIMEOUT);
    }

    public String makeHttpRequest(String urlString, String method, String message) throws IOException {
        return makeHttpRequest(urlString, method, Optional.of(message), DEFAULT_TIMEOUT);
    }

    public String makeHttpRequest(String urlString, String method, String message, int timeout) throws IOException {
        return makeHttpRequest(urlString, method, Optional.of(message), timeout);
    }

    public String makeHttpRequest(String urlString, String method, Optional<String> message, int timeout) throws IOException {
        URL url = new URL(urlString);

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

        conn.setConnectTimeout(timeout);
        conn.setReadTimeout(timeout);

        conn.setRequestMethod(method);

        conn.setDoOutput(true);

        if (message.isPresent()) {
            conn.setDoInput(true);
            PrintWriter pw = new PrintWriter(conn.getOutputStream());
            pw.write(message.get());
            pw.close();
        }

        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        String inputLine;

        StringBuffer strBufRes = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            strBufRes.append(inputLine);
        }

        in.close();

        return strBufRes.toString();
    }

}

@@ -1069,16 +1069,34 @@
 empty(),
+ Optional.empty(),
  DEFAULT
@@ -1293,218 +1293,753 @@
 e), 
-DEFAULT_TIMEOUT);%0A    %7D%0A%0A    public String makeHttpRequest(String urlString, String method, String message, int timeout) throws IOException %7B%0A        return makeHttpRequest(urlString, method, Optional.of(messag
+Optional.empty(), DEFAULT_TIMEOUT);%0A    %7D%0A%0A    public String makeHttpRequest(String urlString, String method, String message, String contentType) throws IOException %7B%0A        return makeHttpRequest(urlString, method, Optional.of(message), Optional.of(contentType), DEFAULT_TIMEOUT);%0A    %7D%0A%0A    public String makeHttpRequest(String urlString, String method, String message, int timeout) throws IOException %7B%0A        return makeHttpRequest(urlString, method, Optional.of(message), Optional.empty(), timeout);%0A    %7D%0A%0A    public String makeHttpRequest(String urlString, String method, String message, String contentType, int timeout) throws IOException %7B%0A        return makeHttpRequest(urlString, method, Optional.of(message), Optional.of(contentTyp
 e), 
@@ -2139,32 +2139,62 @@
 String%3E message,
+ Optional%3CString%3E contentType,
  int timeout) th
@@ -2511,24 +2511,25 @@
 resent()) %7B%0A
+%0A
             
@@ -2547,24 +2547,159 @@
 nput(true);%0A
+%0A            if (contentType.isPresent()) %7B%0A                conn.setRequestProperty(%22Content-type%22, contentType.get());%0A            %7D%0A%0A
             

Allow specifying content type in http request.