001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2024, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014
015package ch.qos.logback.core.joran.spi;
016
017import java.io.*;
018import java.net.HttpURLConnection;
019import java.net.MalformedURLException;
020import java.net.URL;
021import java.nio.charset.StandardCharsets;
022import java.util.HashMap;
023import java.util.Map;
024
025public class HttpUtil {
026
027    URL url;
028
029    public enum RequestMethod {
030        GET,
031        POST;
032    }
033
034
035    HttpURLConnection conn;
036    RequestMethod requestMethod;
037
038    Map<String, String> headerMap = new HashMap<>(2);
039
040    public HttpUtil(RequestMethod requestMethod, URL url) {
041        this.requestMethod = requestMethod;
042        this.url =url;
043    }
044
045    public HttpUtil(RequestMethod requestMethod, String urlStr) throws MalformedURLException {
046        this(requestMethod, new URL(urlStr));
047    }
048
049    Map<String, String> getHeaderMap() {
050        return headerMap;
051    }
052
053    public HttpURLConnection connectTextTxt() {
054        return connectType( "text/txt;charset=utf-8");
055    }
056
057    public HttpURLConnection connectTextPlain() {
058        return connectType("text/plain; charset=utf-8");
059    }
060
061    public HttpURLConnection connectType(String acceptType) {
062        try {
063            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
064            conn.setRequestMethod(requestMethod.name());
065            headerMap.forEach((h, v) -> conn.setRequestProperty(h, v));
066            conn.setRequestProperty("Accept", acceptType);
067
068            if(requestMethod == RequestMethod.POST) {
069                conn.setDoOutput(true);
070            }
071
072            conn.connect();
073            return conn;
074        } catch (IOException e) {
075            e.printStackTrace();
076            return null;
077        }
078    }
079
080    public String readResponse(HttpURLConnection conn) {
081        if(conn == null)
082            return null;
083
084        try {
085            int responseCode = conn.getResponseCode();
086            if(responseCode == HttpURLConnection.HTTP_OK) {
087                return innerReadResponse(conn);
088            } else {
089                System.out.println("status="+ responseCode+ " Failed response");
090                return null;
091            }
092        } catch (IOException e) {
093            System.out.println("url="+ url.toString()+" failed to read status");
094            e.printStackTrace();
095            return  null;
096        }
097    }
098
099    private String innerReadResponse(HttpURLConnection conn) {
100        try (InputStream is = conn.getInputStream()) {
101            BufferedReader in = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
102            String inputLine;
103            StringBuffer buffer = new StringBuffer();
104
105            while ((inputLine = in.readLine()) != null) {
106                buffer.append(inputLine);
107            }
108            return buffer.toString();
109        } catch (IOException e) {
110            e.printStackTrace();
111            return null;
112        }
113    }
114
115    public boolean post(HttpURLConnection conn, String str) {
116        if (conn == null) {
117            System.out.println("null HttpURLConnection object");
118            return false;
119        }
120
121        if(requestMethod != RequestMethod.POST) {
122            System.out.println("Incorrect request method "+requestMethod.name());
123            return false;
124        }
125
126        try (OutputStream os = conn.getOutputStream()) {
127            OutputStreamWriter wr = new OutputStreamWriter(os);
128            wr.write(str);
129            wr.flush();
130            return true;
131        } catch (IOException e) {
132            e.printStackTrace();
133            return false;
134        }
135    }
136
137}