Mentions légales du service

Skip to content
Snippets Groups Projects

Poc/fhir sb 5

Closed luc chatty requested to merge POC/fhirSB-5 into develop
17 files
+ 629
36
Compare changes
  • Side-by-side
  • Inline
Files
17
 
package net.ihe.gazelle.sb.fhir.adapter.hapi.hapihttp;
 
 
import ca.uhn.fhir.rest.api.Constants;
 
import ca.uhn.fhir.rest.api.RequestTypeEnum;
 
import ca.uhn.fhir.rest.client.apache.BaseHttpClient;
 
import ca.uhn.fhir.rest.client.api.Header;
 
import ca.uhn.fhir.rest.client.api.IHttpRequest;
 
import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
 
import org.apache.http.HttpEntity;
 
import org.apache.http.NameValuePair;
 
import org.apache.http.client.entity.UrlEncodedFormEntity;
 
import org.apache.http.entity.ByteArrayEntity;
 
import org.apache.http.message.BasicNameValuePair;
 
 
import javax.ws.rs.client.Client;
 
import javax.ws.rs.client.WebTarget;
 
import java.io.UnsupportedEncodingException;
 
import java.util.ArrayList;
 
import java.util.List;
 
import java.util.Map;
 
 
public class HapiJaxRSHttpClient extends BaseHttpClient {
 
 
 
private Client myClient;
 
 
public HapiJaxRSHttpClient(Client theClient, StringBuilder theUrl, Map<String, List<String>> theIfNoneExistParams, String theIfNoneExistString, RequestTypeEnum theRequestType, List<Header> theHeaders) {
 
super(theUrl, theIfNoneExistParams, theIfNoneExistString, theRequestType, theHeaders);
 
this.myClient = theClient;
 
}
 
 
 
private UrlEncodedFormEntity createFormEntityOverride(List<NameValuePair> parameters) {
 
try {
 
return new UrlEncodedFormEntity(parameters, "UTF-8");
 
} catch (UnsupportedEncodingException e) {
 
throw new InternalErrorException("Server does not support UTF-8 (should not happen)", e);
 
}
 
}
 
 
@Override
 
protected IHttpRequest createHttpRequest() {
 
IHttpRequest retVal = createHttpRequestOverride((HttpEntity)null);
 
return retVal;
 
}
 
 
@Override
 
protected IHttpRequest createHttpRequest(byte[] content) {
 
/*
 
* Note: Be careful about changing which constructor we use for
 
* ByteArrayEntity, as Android's version of HTTPClient doesn't support
 
* the newer ones for whatever reason.
 
*/
 
ByteArrayEntity entity = new ByteArrayEntity(content);
 
IHttpRequest retVal = createHttpRequestOverride(entity);
 
return retVal;
 
}
 
 
private HapiJaxRSHttpRequest createHttpRequestOverride(HttpEntity theEntity) {
 
WebTarget target = myClient.target(myUrl.toString());
 
HapiJaxRSHttpRequest result = new HapiJaxRSHttpRequest(target, myRequestType.name());
 
return result;
 
}
 
 
@Override
 
protected IHttpRequest createHttpRequest(Map<String, List<String>> theParams) {
 
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
 
for (Map.Entry<String, List<String>> nextParam : theParams.entrySet()) {
 
List<String> value = nextParam.getValue();
 
for (String s : value) {
 
parameters.add(new BasicNameValuePair(nextParam.getKey(), s));
 
}
 
}
 
 
UrlEncodedFormEntity entity = createFormEntityOverride(parameters);
 
IHttpRequest retVal = createHttpRequestOverride(entity);
 
return retVal;
 
}
 
 
 
@Override
 
protected IHttpRequest createHttpRequest(String theContents) {
 
/*
 
* We aren't using a StringEntity here because the constructors
 
* supported by Android aren't available in non-Android, and vice versa.
 
* Since we add the content type header manually, it makes no difference
 
* which one we use anyhow.
 
*/
 
ByteArrayEntity entity = new ByteArrayEntity(theContents.getBytes(Constants.CHARSET_UTF8));
 
IHttpRequest retVal = createHttpRequestOverride(entity);
 
return retVal;
 
}
 
 
}
Loading