• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

opensrp / opensrp-server-common / #25

pending completion
#25

Pull #41

Github

web-flow
Merge b0efe0dc4 into 2b321155f
Pull Request #41: Security scan configurations

1 of 1 new or added line in 1 file covered. (100.0%)

603 of 906 relevant lines covered (66.56%)

0.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/org/opensrp/common/util/HttpAgent.java
1
package org.opensrp.common.util;
2

3
import org.apache.commons.io.IOUtils;
4
import org.apache.http.HttpStatus;
5
import org.apache.http.NameValuePair;
6
import org.apache.http.client.entity.UrlEncodedFormEntity;
7
import org.apache.http.client.methods.HttpGet;
8
import org.apache.http.client.methods.HttpPost;
9
import org.apache.http.client.methods.HttpPut;
10
import org.apache.http.conn.ClientConnectionManager;
11
import org.apache.http.conn.scheme.PlainSocketFactory;
12
import org.apache.http.conn.scheme.Scheme;
13
import org.apache.http.conn.scheme.SchemeRegistry;
14
import org.apache.http.conn.scheme.SocketFactory;
15
import org.apache.http.conn.ssl.SSLSocketFactory;
16
import org.apache.http.conn.ssl.X509HostnameVerifier;
17
import org.apache.http.entity.StringEntity;
18
import org.apache.http.impl.client.DefaultHttpClient;
19
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
20
import org.apache.http.message.BasicNameValuePair;
21
import org.apache.http.params.BasicHttpParams;
22
import org.apache.http.params.CoreConnectionPNames;
23
import org.apache.http.params.HttpConnectionParams;
24
import org.apache.http.protocol.HTTP;
25
import org.bouncycastle.jce.provider.BouncyCastleProvider;
26
import org.springframework.stereotype.Component;
27

28
import java.io.InputStream;
29
import java.security.KeyStore;
30
import java.security.Security;
31
import java.util.ArrayList;
32
import java.util.List;
33
import java.util.Map;
34

35
/**
36
 * This class is marked as deprecated
37
 * Use HttpUtil to access the same functionality provided by this class
38
*/
39
@Deprecated(since = "2.1.2-SNAPSHOT", forRemoval = true)
40
@Component
41
public class HttpAgent {
42

43
    private final DefaultHttpClient httpClient;
44

45
    public HttpAgent() {
×
46
        BasicHttpParams basicHttpParams = new BasicHttpParams();
×
47
        HttpConnectionParams.setConnectionTimeout(basicHttpParams, 30000);
×
48
        HttpConnectionParams.setSoTimeout(basicHttpParams, 60000);
×
49

50
        SchemeRegistry registry = new SchemeRegistry();
×
51
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
×
52
        registry.register(new Scheme("https", sslSocketFactoryWithDrishtiCertificate(), 443));
×
53

54
        ClientConnectionManager connectionManager = new ThreadSafeClientConnManager(basicHttpParams, registry);
×
55
        httpClient = new DefaultHttpClient(connectionManager, basicHttpParams);
×
56
    }
×
57

58
    public HttpResponse post(String url, String data, String contentType) {
59
        HttpPost request = new HttpPost(url);
×
60
        try {
61
            request.setHeader(HTTP.CONTENT_TYPE, contentType);
×
62
            StringEntity entity = new StringEntity(data);
×
63
            entity.setContentEncoding(contentType);
×
64
            request.setEntity(entity);
×
65
            org.apache.http.HttpResponse response = httpClient.execute(request);
×
66
            return new HttpResponse(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK,
×
67
                    IOUtils.toString(response.getEntity().getContent()));
×
68
        } catch (Exception e) {
×
69
            throw new RuntimeException(e);
×
70
        }
71
    }
72

73
    public HttpResponse put(String url, Map<String, String> formParams) {
74
        HttpPut request = new HttpPut(url);
×
75
        try {
76
            List<NameValuePair> urlParameters = new ArrayList<>();
×
77
            for (String param : formParams.keySet()) {
×
78
                urlParameters.add(new BasicNameValuePair(param, formParams.get(param)));
×
79
            }
×
80
            request.setEntity(new UrlEncodedFormEntity(urlParameters));
×
81
            org.apache.http.HttpResponse response = httpClient.execute(request);
×
82
            return new HttpResponse(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK,
×
83
                    IOUtils.toString(response.getEntity().getContent()));
×
84
        } catch (Exception e) {
×
85
            throw new RuntimeException(e);
×
86
        }
87
    }
88

89
    public HttpResponse get(String url) {
90
        HttpGet request = new HttpGet(url);
×
91
        try {
92
            org.apache.http.HttpResponse response = httpClient.execute(request);
×
93
            return new HttpResponse(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK,
×
94
                    IOUtils.toString(response.getEntity().getContent()));
×
95
        } catch (Exception e) {
×
96
            throw new RuntimeException(e);
×
97
        }
98
    }
99

100
    public HttpResponse getWithSocketTimeout(String url) {
101
        HttpGet request = new HttpGet(url);
×
102
        try {
103
            httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 0);
×
104
            org.apache.http.HttpResponse response = httpClient.execute(request);
×
105
            return new HttpResponse(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK,
×
106
                    IOUtils.toString(response.getEntity().getContent()));
×
107
        } catch (Exception e) {
×
108
            throw new RuntimeException(e);
×
109
        }
110
    }
111

112
    private SocketFactory sslSocketFactoryWithDrishtiCertificate() {
113
        try {
114
            Security.addProvider(new BouncyCastleProvider());
×
115
            KeyStore trustedKeystore = KeyStore.getInstance("BKS");
×
116
            InputStream inputStream = this.getClass().getResourceAsStream("/drishti_client.keystore");
×
117
            try {
118
                trustedKeystore.load(inputStream, "".toCharArray());
×
119
            } finally {
120
                inputStream.close();
×
121
            }
122

123
            SSLSocketFactory socketFactory = new SSLSocketFactory(trustedKeystore);
×
124
            final X509HostnameVerifier oldVerifier = SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
×
125
            socketFactory.setHostnameVerifier(oldVerifier);
×
126
            return socketFactory;
×
127
        } catch (Exception e) {
×
128
            throw new AssertionError(e);
×
129
        }
130
    }
131
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc