|
@@ -22,34 +22,41 @@
|
|
|
|
|
|
package password.pwm.ws.client.rest.form;
|
|
|
|
|
|
-import org.apache.http.HttpResponse;
|
|
|
-import org.apache.http.client.methods.HttpPost;
|
|
|
-import org.apache.http.entity.StringEntity;
|
|
|
-import org.apache.http.util.EntityUtils;
|
|
|
import password.pwm.PwmApplication;
|
|
|
import password.pwm.PwmConstants;
|
|
|
+import password.pwm.bean.SessionLabel;
|
|
|
+import password.pwm.config.Configuration;
|
|
|
import password.pwm.config.PwmSetting;
|
|
|
import password.pwm.config.value.data.RemoteWebServiceConfiguration;
|
|
|
import password.pwm.error.ErrorInformation;
|
|
|
import password.pwm.error.PwmError;
|
|
|
import password.pwm.error.PwmUnrecoverableException;
|
|
|
+import password.pwm.http.HttpHeader;
|
|
|
+import password.pwm.http.HttpMethod;
|
|
|
import password.pwm.http.client.PwmHttpClient;
|
|
|
+import password.pwm.http.client.PwmHttpClientConfiguration;
|
|
|
+import password.pwm.http.client.PwmHttpClientRequest;
|
|
|
+import password.pwm.http.client.PwmHttpClientResponse;
|
|
|
import password.pwm.util.java.JsonUtil;
|
|
|
import password.pwm.util.logging.PwmLogger;
|
|
|
|
|
|
-import java.io.IOException;
|
|
|
+import java.security.cert.X509Certificate;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Locale;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
public class RestFormDataClient {
|
|
|
|
|
|
private static final PwmLogger LOGGER = PwmLogger.forClass(RestFormDataClient.class);
|
|
|
|
|
|
private final PwmApplication pwmApplication;
|
|
|
+ private final SessionLabel sessionLabel;
|
|
|
private RemoteWebServiceConfiguration remoteWebServiceConfiguration;
|
|
|
|
|
|
- public RestFormDataClient(final PwmApplication pwmApplication)
|
|
|
+ public RestFormDataClient(final PwmApplication pwmApplication, final SessionLabel sessionLabel)
|
|
|
{
|
|
|
+ this.sessionLabel = sessionLabel;
|
|
|
this.pwmApplication = pwmApplication;
|
|
|
final List<RemoteWebServiceConfiguration> values = pwmApplication.getConfig().readSettingAsRemoteWebService(PwmSetting.EXTERNAL_REMOTE_DATA_URL);
|
|
|
if (values != null && !values.isEmpty()) {
|
|
@@ -67,32 +74,36 @@ public class RestFormDataClient {
|
|
|
)
|
|
|
throws PwmUnrecoverableException
|
|
|
{
|
|
|
- final HttpPost httpPost = new HttpPost(remoteWebServiceConfiguration.getUrl());
|
|
|
- httpPost.setHeader("Accept", PwmConstants.AcceptValue.json.getHeaderValue());
|
|
|
+ final Map<String,String> httpHeaders = new LinkedHashMap<>();
|
|
|
+ httpHeaders.put(HttpHeader.Accept.getHttpName(), PwmConstants.AcceptValue.json.getHeaderValue());
|
|
|
+ httpHeaders.put(HttpHeader.Content_Type.getHttpName(), PwmConstants.ContentTypeValue.json.getHeaderValue());
|
|
|
if (locale != null) {
|
|
|
- httpPost.setHeader("Accept-Locale", locale.toString());
|
|
|
+ httpHeaders.put(HttpHeader.Accept_Language.getHttpName(), locale.toString());
|
|
|
}
|
|
|
- httpPost.setHeader("Content-Type", PwmConstants.ContentTypeValue.json.getHeaderValue());
|
|
|
|
|
|
final String jsonRequestBody = JsonUtil.serialize(formDataRequestBean);
|
|
|
|
|
|
- final HttpResponse httpResponse;
|
|
|
+ final PwmHttpClientRequest pwmHttpClientRequest = new PwmHttpClientRequest(
|
|
|
+ HttpMethod.POST,
|
|
|
+ remoteWebServiceConfiguration.getUrl(),
|
|
|
+ jsonRequestBody,
|
|
|
+ httpHeaders
|
|
|
+
|
|
|
+ );
|
|
|
+
|
|
|
+ final PwmHttpClientResponse httpResponse;
|
|
|
try {
|
|
|
- final StringEntity stringEntity = new StringEntity(jsonRequestBody);
|
|
|
- stringEntity.setContentType(PwmConstants.AcceptValue.json.getHeaderValue());
|
|
|
- httpPost.setEntity(stringEntity);
|
|
|
- LOGGER.debug("beginning external rest call to: " + httpPost.toString() + ", body: " + jsonRequestBody);
|
|
|
- httpResponse = PwmHttpClient.getHttpClient(pwmApplication.getConfig()).execute(httpPost);
|
|
|
- final String responseBody = EntityUtils.toString(httpResponse.getEntity());
|
|
|
- LOGGER.trace("external rest call returned: " + httpResponse.getStatusLine().toString() + ", body: " + responseBody);
|
|
|
- if (httpResponse.getStatusLine().getStatusCode() != 200) {
|
|
|
- final String errorMsg = "received non-200 response code (" + httpResponse.getStatusLine().getStatusCode() + ") when executing web-service";
|
|
|
+ httpResponse = getHttpClient(pwmApplication.getConfig()).makeRequest(pwmHttpClientRequest);
|
|
|
+ final String responseBody = httpResponse.getBody();
|
|
|
+ LOGGER.trace("external rest call returned: " + httpResponse.getStatusPhrase() + ", body: " + responseBody);
|
|
|
+ if (httpResponse.getStatusCode() != 200) {
|
|
|
+ final String errorMsg = "received non-200 response code (" + httpResponse.getStatusCode() + ") when executing web-service";
|
|
|
LOGGER.error(errorMsg);
|
|
|
throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_SERVICE_UNREACHABLE, errorMsg));
|
|
|
}
|
|
|
final FormDataResponseBean formDataResponseBean = JsonUtil.deserialize(responseBody, FormDataResponseBean.class);
|
|
|
return formDataResponseBean;
|
|
|
- } catch (IOException e) {
|
|
|
+ } catch (PwmUnrecoverableException e) {
|
|
|
final String errorMsg = "http response error while executing external rest call, error: " + e.getMessage();
|
|
|
LOGGER.error(errorMsg);
|
|
|
throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_SERVICE_UNREACHABLE, errorMsg),e);
|
|
@@ -100,4 +111,20 @@ public class RestFormDataClient {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ private PwmHttpClient getHttpClient(final Configuration configuration)
|
|
|
+ throws PwmUnrecoverableException
|
|
|
+ {
|
|
|
+ final List<RemoteWebServiceConfiguration> webServiceConfigurations = configuration.readSettingAsRemoteWebService(PwmSetting.EXTERNAL_REMOTE_DATA_URL);
|
|
|
+
|
|
|
+ final X509Certificate[] certificates;
|
|
|
+ certificates = webServiceConfigurations != null && webServiceConfigurations.isEmpty()
|
|
|
+ ? webServiceConfigurations.iterator().next().getCertificates()
|
|
|
+ : null;
|
|
|
+
|
|
|
+ final PwmHttpClientConfiguration pwmHttpClientConfiguration = new PwmHttpClientConfiguration.Builder()
|
|
|
+ .setCertificate(certificates)
|
|
|
+ .create();
|
|
|
+ return new PwmHttpClient(pwmApplication, null, pwmHttpClientConfiguration);
|
|
|
+ }
|
|
|
+
|
|
|
}
|