BrightSide Workbench Full Report + Source Code
ElephantPost.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.http;
20 
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import javax.servlet.http.HttpServletRequest;
26 import org.turro.string.Strings;
27 import org.apache.hc.client5.http.classic.methods.HttpPost;
28 import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
29 import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
30 import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
31 import org.apache.hc.client5.http.impl.classic.HttpClients;
32 import org.apache.hc.core5.http.HttpEntity;
33 import org.apache.hc.core5.http.NameValuePair;
34 import org.apache.hc.core5.http.message.BasicNameValuePair;
35 import org.turro.action.Secrets;
36 
41 public class ElephantPost {
42 
43  private final String serverUrl;
44  private final HashMap<String, String> parameters;
45 
46  public ElephantPost(String serverUrl) {
47  this.serverUrl = serverUrl;
48  parameters = new HashMap<>();
49  }
50 
51  public void addParameter(String name, String value) {
52  parameters.put(name, value);
53  }
54 
55  public ElephantResponse doPost() throws IOException {
56  if(serverUrl == null || parameters.isEmpty()) {
57  return null;
58  }
59  List<NameValuePair> nvps = new ArrayList<>();
60  for(String key : parameters.keySet()) {
61  String value = parameters.get(key);
62  if(!Strings.isBlank(value)) {
63  nvps.add(new BasicNameValuePair(key, value));
64  }
65  }
66  nvps.add(new BasicNameValuePair("skey", Secrets.getSecret("key=post")));
67  HttpPost httpPost = new HttpPost(serverUrl);
68  httpPost.setEntity(new UrlEncodedFormEntity(nvps));
69  try (CloseableHttpClient closeableClient = (CloseableHttpClient) HttpClients.createDefault()) {
70  try (CloseableHttpResponse closeableResponse = (CloseableHttpResponse) closeableClient.execute(httpPost)) {
71  HttpEntity resEntity = closeableResponse.getEntity();
72  return ElephantResponse.getResponse(resEntity);
73  }
74  }
75  }
76 
77  public static boolean isValid(HttpServletRequest request) {
78  return Secrets.getSecret("key=post").equals(request.getParameter("skey"));
79  }
80 
81 }
static String getSecret(String keys)
Definition: Secrets.java:37
ElephantResponse doPost()
void addParameter(String name, String value)
ElephantPost(String serverUrl)
static boolean isValid(HttpServletRequest request)
static ElephantResponse getResponse(HttpEntity entity)