001package org.hl7.fhir.r5.utils.client;
002
003/*-
004 * #%L
005 * org.hl7.fhir.r5
006 * %%
007 * Copyright (C) 2014 - 2019 Health Level 7
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023
024
025/*
026  Copyright (c) 2011+, HL7, Inc.
027  All rights reserved.
028
029  Redistribution and use in source and binary forms, with or without modification, 
030  are permitted provided that the following conditions are met:
031
032 * Redistributions of source code must retain the above copyright notice, this 
033     list of conditions and the following disclaimer.
034 * Redistributions in binary form must reproduce the above copyright notice, 
035     this list of conditions and the following disclaimer in the documentation 
036     and/or other materials provided with the distribution.
037 * Neither the name of HL7 nor the names of its contributors may be used to 
038     endorse or promote products derived from this software without specific 
039     prior written permission.
040
041  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
042  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
043  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
044  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
045  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
046  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
047  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
048  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
049  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
050  POSSIBILITY OF SUCH DAMAGE.
051
052 */
053
054
055import java.io.ByteArrayOutputStream;
056import java.io.IOException;
057import java.io.InputStream;
058import java.io.OutputStreamWriter;
059import java.io.UnsupportedEncodingException;
060import java.net.HttpURLConnection;
061import java.net.MalformedURLException;
062import java.net.URI;
063import java.net.URLConnection;
064import java.nio.charset.StandardCharsets;
065import java.text.ParseException;
066import java.text.SimpleDateFormat;
067import java.util.ArrayList;
068import java.util.Calendar;
069import java.util.Date;
070import java.util.List;
071import java.util.Locale;
072import java.util.Map;
073
074import org.apache.commons.codec.binary.Base64;
075import org.apache.commons.io.IOUtils;
076import org.apache.commons.lang3.StringUtils;
077import org.apache.http.Header;
078import org.apache.http.HttpEntityEnclosingRequest;
079import org.apache.http.HttpHost;
080import org.apache.http.HttpRequest;
081import org.apache.http.HttpResponse;
082import org.apache.http.client.HttpClient;
083import org.apache.http.client.methods.HttpDelete;
084import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
085import org.apache.http.client.methods.HttpGet;
086import org.apache.http.client.methods.HttpOptions;
087import org.apache.http.client.methods.HttpPost;
088import org.apache.http.client.methods.HttpPut;
089import org.apache.http.client.methods.HttpUriRequest;
090import org.apache.http.conn.params.ConnRoutePNames;
091import org.apache.http.entity.ByteArrayEntity;
092import org.apache.http.impl.client.DefaultHttpClient;
093import org.apache.http.params.HttpConnectionParams;
094import org.apache.http.params.HttpParams;
095import org.hl7.fhir.r5.formats.IParser;
096import org.hl7.fhir.r5.formats.IParser.OutputStyle;
097import org.hl7.fhir.r5.formats.JsonParser;
098import org.hl7.fhir.r5.formats.XmlParser;
099import org.hl7.fhir.r5.model.Bundle;
100import org.hl7.fhir.r5.model.OperationOutcome;
101import org.hl7.fhir.r5.model.OperationOutcome.IssueSeverity;
102import org.hl7.fhir.r5.model.OperationOutcome.OperationOutcomeIssueComponent;
103import org.hl7.fhir.r5.model.Resource;
104import org.hl7.fhir.r5.model.ResourceType;
105import org.hl7.fhir.r5.utils.ResourceUtilities;
106
107/**
108 * Helper class handling lower level HTTP transport concerns.
109 * TODO Document methods.
110 * @author Claude Nanjo
111 */
112public class ClientUtils {
113
114  public static final String DEFAULT_CHARSET = "UTF-8";
115  public static final String HEADER_LOCATION = "location";
116
117  private HttpHost proxy;
118  private int timeout = 5000;
119  private String username;
120  private String password;
121  private ToolingClientLogger logger;
122
123  public HttpHost getProxy() {
124    return proxy;
125  }
126
127  public void setProxy(HttpHost proxy) {
128    this.proxy = proxy;
129  }
130
131  public int getTimeout() {
132    return timeout;
133  }
134
135  public void setTimeout(int timeout) {
136    this.timeout = timeout;
137  }
138
139  public String getUsername() {
140    return username;
141  }
142
143  public void setUsername(String username) {
144    this.username = username;
145  }
146
147  public String getPassword() {
148    return password;
149  }
150
151  public void setPassword(String password) {
152    this.password = password;
153  }
154
155  public <T extends Resource> ResourceRequest<T> issueOptionsRequest(URI optionsUri, String resourceFormat) {
156    HttpOptions options = new HttpOptions(optionsUri);
157    return issueResourceRequest(resourceFormat, options);
158  }
159
160  public <T extends Resource> ResourceRequest<T> issueGetResourceRequest(URI resourceUri, String resourceFormat) {
161    HttpGet httpget = new HttpGet(resourceUri);
162    return issueResourceRequest(resourceFormat, httpget);
163  }
164
165  public <T extends Resource> ResourceRequest<T> issuePutRequest(URI resourceUri, byte[] payload, String resourceFormat, List<Header> headers) {
166    HttpPut httpPut = new HttpPut(resourceUri);
167    return issueResourceRequest(resourceFormat, httpPut, payload, headers);
168  }
169
170  public <T extends Resource> ResourceRequest<T> issuePutRequest(URI resourceUri, byte[] payload, String resourceFormat) {
171    HttpPut httpPut = new HttpPut(resourceUri);
172    return issueResourceRequest(resourceFormat, httpPut, payload, null);
173  }
174
175  public <T extends Resource> ResourceRequest<T> issuePostRequest(URI resourceUri, byte[] payload, String resourceFormat, List<Header> headers) {
176    HttpPost httpPost = new HttpPost(resourceUri);
177    return issueResourceRequest(resourceFormat, httpPost, payload, headers);
178  }
179
180
181  public <T extends Resource> ResourceRequest<T> issuePostRequest(URI resourceUri, byte[] payload, String resourceFormat) {
182    return issuePostRequest(resourceUri, payload, resourceFormat, null);
183  }
184
185  public Bundle issueGetFeedRequest(URI resourceUri, String resourceFormat) {
186    HttpGet httpget = new HttpGet(resourceUri);
187    configureFhirRequest(httpget, resourceFormat);
188    HttpResponse response = sendRequest(httpget);
189    return unmarshalReference(response, resourceFormat);
190  }
191
192  private void setAuth(HttpRequest httpget) {
193    if (password != null) {
194      try {
195        byte[] b = Base64.encodeBase64((username+":"+password).getBytes("ASCII"));
196        String b64 = new String(b, StandardCharsets.US_ASCII);
197        httpget.setHeader("Authorization", "Basic " + b64);
198      } catch (UnsupportedEncodingException e) {
199      }
200    }
201  }
202
203  public Bundle postBatchRequest(URI resourceUri, byte[] payload, String resourceFormat) {
204    HttpPost httpPost = new HttpPost(resourceUri);
205    configureFhirRequest(httpPost, resourceFormat);
206    HttpResponse response = sendPayload(httpPost, payload, proxy);
207    return unmarshalFeed(response, resourceFormat);
208  }
209
210  public boolean issueDeleteRequest(URI resourceUri) {
211    HttpDelete deleteRequest = new HttpDelete(resourceUri);
212    HttpResponse response = sendRequest(deleteRequest);
213    int responseStatusCode = response.getStatusLine().getStatusCode();
214    boolean deletionSuccessful = false;
215    if(responseStatusCode == 204) {
216      deletionSuccessful = true;
217    }
218    return deletionSuccessful;
219  }
220
221  /***********************************************************
222   * Request/Response Helper methods
223   ***********************************************************/
224
225  protected <T extends Resource> ResourceRequest<T> issueResourceRequest(String resourceFormat, HttpUriRequest request) {
226    return issueResourceRequest(resourceFormat, request, null);
227  }
228
229  /**
230   * @param resourceFormat
231   * @param options
232   * @return
233   */
234  protected <T extends Resource> ResourceRequest<T> issueResourceRequest(String resourceFormat, HttpUriRequest request, byte[] payload) {
235    return issueResourceRequest(resourceFormat, request, payload, null);
236  }
237
238  /**
239   * @param resourceFormat
240   * @param options
241   * @return
242   */
243  protected <T extends Resource> ResourceRequest<T> issueResourceRequest(String resourceFormat, HttpUriRequest request, byte[] payload, List<Header> headers) {
244    configureFhirRequest(request, resourceFormat, headers);
245    HttpResponse response = null;
246    if(request instanceof HttpEntityEnclosingRequest && payload != null) {
247      response = sendPayload((HttpEntityEnclosingRequestBase)request, payload, proxy);
248    } else if (request instanceof HttpEntityEnclosingRequest && payload == null){
249      throw new EFhirClientException("PUT and POST requests require a non-null payload");
250    } else {
251      response = sendRequest(request);
252    }
253    T resource = unmarshalReference(response, resourceFormat);
254    return new ResourceRequest<T>(resource, response.getStatusLine().getStatusCode(), getLocationHeader(response));
255  }
256
257
258  /**
259   * Method adds required request headers.
260   * TODO handle JSON request as well.
261   * 
262   * @param request
263   */
264  protected void configureFhirRequest(HttpRequest request, String format) {
265    configureFhirRequest(request, format, null);
266  }
267
268  /**
269   * Method adds required request headers.
270   * TODO handle JSON request as well.
271   * 
272   * @param request
273   */
274  protected void configureFhirRequest(HttpRequest request, String format, List<Header> headers) {
275    request.addHeader("User-Agent", "Java FHIR Client for FHIR");
276
277    if (format != null) {               
278      request.addHeader("Accept",format);
279      request.addHeader("Content-Type", format + ";charset=" + DEFAULT_CHARSET);
280    }
281    request.addHeader("Accept-Charset", DEFAULT_CHARSET);
282    if(headers != null) {
283      for(Header header : headers) {
284        request.addHeader(header);
285      }
286    }
287    setAuth(request);
288  }
289
290  /**
291   * Method posts request payload
292   * 
293   * @param request
294   * @param payload
295   * @return
296   */
297  protected HttpResponse sendPayload(HttpEntityEnclosingRequestBase request, byte[] payload, HttpHost proxy) {
298    HttpResponse response = null;
299    try {
300      HttpClient httpclient = new DefaultHttpClient();
301      if(proxy != null) {
302        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
303      }
304      request.setEntity(new ByteArrayEntity(payload));
305      log(request);
306      response = httpclient.execute(request);
307    } catch(IOException ioe) {
308      throw new EFhirClientException("Error sending HTTP Post/Put Payload", ioe);
309    }
310    return response;
311  }
312
313  /**
314   * 
315   * @param request
316   * @param payload
317   * @return
318   */
319  protected HttpResponse sendRequest(HttpUriRequest request) {
320    HttpResponse response = null;
321    try {
322      HttpClient httpclient = new DefaultHttpClient();
323      log(request);
324      HttpParams params = httpclient.getParams();
325      HttpConnectionParams.setConnectionTimeout(params, timeout);
326      HttpConnectionParams.setSoTimeout(params, timeout);
327      if(proxy != null) {
328        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
329      }
330      response = httpclient.execute(request);
331    } catch(IOException ioe) {
332      throw new EFhirClientException("Error sending Http Request: "+ioe.getMessage(), ioe);
333    }
334    return response;
335  }
336
337
338  /**
339   * Unmarshals a resource from the response stream.
340   * 
341   * @param response
342   * @return
343   */
344  @SuppressWarnings("unchecked")
345  protected <T extends Resource> T unmarshalReference(HttpResponse response, String format) {
346    T resource = null;
347    OperationOutcome error = null;
348    byte[] cnt = log(response);
349    if (cnt != null) {
350      try {
351        resource = (T)getParser(format).parse(cnt);
352        if (resource instanceof OperationOutcome && hasError((OperationOutcome)resource)) {
353          error = (OperationOutcome) resource;
354        }
355      } catch(IOException ioe) {
356        throw new EFhirClientException("Error reading Http Response: "+ioe.getMessage(), ioe);
357      } catch(Exception e) {
358        throw new EFhirClientException("Error parsing response message: "+e.getMessage(), e);
359      }
360    }
361    if(error != null) {
362      throw new EFhirClientException("Error from server: "+ResourceUtilities.getErrorDescription(error), error);
363    }
364    return resource;
365  }
366
367  /**
368   * Unmarshals Bundle from response stream.
369   * 
370   * @param response
371   * @return
372   */
373  protected Bundle unmarshalFeed(HttpResponse response, String format) {
374    Bundle feed = null;
375    byte[] cnt = log(response);
376    String contentType = response.getHeaders("Content-Type")[0].getValue();
377    OperationOutcome error = null;
378    try {
379      if (cnt != null) {
380        if(contentType.contains(ResourceFormat.RESOURCE_XML.getHeader()) || contentType.contains("text/xml+fhir")) {
381          Resource rf = getParser(format).parse(cnt);
382          if (rf instanceof Bundle)
383            feed = (Bundle) rf;
384          else if (rf instanceof OperationOutcome && hasError((OperationOutcome) rf)) {
385            error = (OperationOutcome) rf;
386          } else {
387            throw new EFhirClientException("Error reading server response: a resource was returned instead");
388          }
389        }
390      }
391    } catch(IOException ioe) {
392      throw new EFhirClientException("Error reading Http Response", ioe);
393    } catch(Exception e) {
394      throw new EFhirClientException("Error parsing response message", e);
395    }
396    if(error != null) {
397      throw new EFhirClientException("Error from server: "+ResourceUtilities.getErrorDescription(error), error);
398    }
399    return feed;
400  }
401
402  private boolean hasError(OperationOutcome oo) {
403    for (OperationOutcomeIssueComponent t : oo.getIssue())
404      if (t.getSeverity() == IssueSeverity.ERROR || t.getSeverity() == IssueSeverity.FATAL)
405        return true;
406    return false;
407  }
408
409  protected String getLocationHeader(HttpResponse response) {
410    String location = null;
411    if(response.getHeaders("location").length > 0) {//TODO Distinguish between both cases if necessary
412      location = response.getHeaders("location")[0].getValue();
413    } else if(response.getHeaders("content-location").length > 0) {
414      location = response.getHeaders("content-location")[0].getValue();
415    }
416    return location;
417  }
418
419
420  /*****************************************************************
421   * Client connection methods
422   * ***************************************************************/
423
424  public HttpURLConnection buildConnection(URI baseServiceUri, String tail) {
425    try {
426      HttpURLConnection client = (HttpURLConnection) baseServiceUri.resolve(tail).toURL().openConnection();
427      return client;
428    } catch(MalformedURLException mue) {
429      throw new EFhirClientException("Invalid Service URL", mue);
430    } catch(IOException ioe) {
431      throw new EFhirClientException("Unable to establish connection to server: " + baseServiceUri.toString() + tail, ioe);
432    }
433  }
434
435  public HttpURLConnection buildConnection(URI baseServiceUri, ResourceType resourceType, String id) {
436    return buildConnection(baseServiceUri, ResourceAddress.buildRelativePathFromResourceType(resourceType, id));
437  }
438
439  /******************************************************************
440   * Other general helper methods
441   * ****************************************************************/
442
443
444  public  <T extends Resource>  byte[] getResourceAsByteArray(T resource, boolean pretty, boolean isJson) {
445    ByteArrayOutputStream baos = null;
446    byte[] byteArray = null;
447    try {
448      baos = new ByteArrayOutputStream();
449      IParser parser = null;
450      if(isJson) {
451        parser = new JsonParser();
452      } else {
453        parser = new XmlParser();
454      }
455      parser.setOutputStyle(pretty ? OutputStyle.PRETTY : OutputStyle.NORMAL);
456      parser.compose(baos, resource);
457      baos.close();
458      byteArray =  baos.toByteArray();
459      baos.close();
460    } catch (Exception e) {
461      try{
462        baos.close();
463      }catch(Exception ex) {
464        throw new EFhirClientException("Error closing output stream", ex);
465      }
466      throw new EFhirClientException("Error converting output stream to byte array", e);
467    }
468    return byteArray;
469  }
470
471  public  byte[] getFeedAsByteArray(Bundle feed, boolean pretty, boolean isJson) {
472    ByteArrayOutputStream baos = null;
473    byte[] byteArray = null;
474    try {
475      baos = new ByteArrayOutputStream();
476      IParser parser = null;
477      if(isJson) {
478        parser = new JsonParser();
479      } else {
480        parser = new XmlParser();
481      }
482      parser.setOutputStyle(pretty ? OutputStyle.PRETTY : OutputStyle.NORMAL);
483      parser.compose(baos, feed);
484      baos.close();
485      byteArray =  baos.toByteArray();
486      baos.close();
487    } catch (Exception e) {
488      try{
489        baos.close();
490      }catch(Exception ex) {
491        throw new EFhirClientException("Error closing output stream", ex);
492      }
493      throw new EFhirClientException("Error converting output stream to byte array", e);
494    }
495    return byteArray;
496  }
497
498  public Calendar getLastModifiedResponseHeaderAsCalendarObject(URLConnection serverConnection) {
499    String dateTime = null;
500    try {
501      dateTime = serverConnection.getHeaderField("Last-Modified");
502      SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", new Locale("en", "US"));
503      Date lastModifiedTimestamp = format.parse(dateTime);
504      Calendar calendar=Calendar.getInstance();
505      calendar.setTime(lastModifiedTimestamp);
506      return calendar;
507    } catch(ParseException pe) {
508      throw new EFhirClientException("Error parsing Last-Modified response header " + dateTime, pe);
509    }
510  }
511
512  protected IParser getParser(String format) {
513    if(StringUtils.isBlank(format)) {
514      format = ResourceFormat.RESOURCE_XML.getHeader();
515    }
516    if(format.equalsIgnoreCase("json") || format.equalsIgnoreCase(ResourceFormat.RESOURCE_JSON.getHeader()) || format.equalsIgnoreCase(ResourceFormat.RESOURCE_JSON.getHeader())) {
517      return new JsonParser();
518    } else if(format.equalsIgnoreCase("xml") || format.equalsIgnoreCase(ResourceFormat.RESOURCE_XML.getHeader()) || format.equalsIgnoreCase(ResourceFormat.RESOURCE_XML.getHeader())) {
519      return new XmlParser();
520    } else {
521      throw new EFhirClientException("Invalid format: " + format);
522    }
523  }
524
525  public Bundle issuePostFeedRequest(URI resourceUri, Map<String, String> parameters, String resourceName, Resource resource, String resourceFormat) throws IOException {
526    HttpPost httppost = new HttpPost(resourceUri);
527    String boundary = "----WebKitFormBoundarykbMUo6H8QaUnYtRy";
528    httppost.addHeader("Content-Type", "multipart/form-data; boundary="+boundary);
529    httppost.addHeader("Accept", resourceFormat);
530    configureFhirRequest(httppost, null);
531    HttpResponse response = sendPayload(httppost, encodeFormSubmission(parameters, resourceName, resource, boundary));
532    return unmarshalFeed(response, resourceFormat);
533  }
534
535  private byte[] encodeFormSubmission(Map<String, String> parameters, String resourceName, Resource resource, String boundary) throws IOException {
536    ByteArrayOutputStream b = new ByteArrayOutputStream();
537    OutputStreamWriter w = new OutputStreamWriter(b, "UTF-8");  
538    for (String name : parameters.keySet()) {
539      w.write("--");
540      w.write(boundary);
541      w.write("\r\nContent-Disposition: form-data; name=\""+name+"\"\r\n\r\n");
542      w.write(parameters.get(name)+"\r\n");
543    }
544    w.write("--");
545    w.write(boundary);
546    w.write("\r\nContent-Disposition: form-data; name=\""+resourceName+"\"\r\n\r\n");
547    w.close(); 
548    JsonParser json = new JsonParser();
549    json.setOutputStyle(OutputStyle.NORMAL);
550    json.compose(b, resource);
551    b.close();
552    w = new OutputStreamWriter(b, "UTF-8");  
553    w.write("\r\n--");
554    w.write(boundary);
555    w.write("--");
556    w.close();
557    return b.toByteArray();
558  }
559
560  /**
561   * Method posts request payload
562   * 
563   * @param request
564   * @param payload
565   * @return
566   */
567  protected HttpResponse sendPayload(HttpEntityEnclosingRequestBase request, byte[] payload) {
568    HttpResponse response = null;
569    try {
570      log(request);
571      HttpClient httpclient = new DefaultHttpClient();
572      request.setEntity(new ByteArrayEntity(payload));
573      response = httpclient.execute(request);
574      log(response);
575    } catch(IOException ioe) {
576      throw new EFhirClientException("Error sending HTTP Post/Put Payload: "+ioe.getMessage(), ioe);
577    }
578    return response;
579  }
580
581  private void log(HttpUriRequest request) {
582    if (logger != null) {
583      List<String> headers = new ArrayList<>();
584      for (Header h : request.getAllHeaders()) {
585        headers.add(h.toString());
586      }
587      logger.logRequest(request.getMethod(), request.getURI().toString(), headers, null);
588    }    
589  }
590  private void log(HttpEntityEnclosingRequestBase request)  {
591    if (logger != null) {
592      List<String> headers = new ArrayList<>();
593      for (Header h : request.getAllHeaders()) {
594        headers.add(h.toString());
595      }
596      byte[] cnt = null;
597      InputStream s;
598      try {
599        s = request.getEntity().getContent();
600        cnt = IOUtils.toByteArray(s);
601        s.close();
602      } catch (Exception e) {
603      }
604      logger.logRequest(request.getMethod(), request.getURI().toString(), headers, cnt);
605    }    
606  }  
607  
608  private byte[] log(HttpResponse response) {
609    byte[] cnt = null;
610    try {
611      InputStream s = response.getEntity().getContent();
612      cnt = IOUtils.toByteArray(s);
613      s.close();
614    } catch (Exception e) {
615    }
616    if (logger != null) {
617      List<String> headers = new ArrayList<>();
618      for (Header h : response.getAllHeaders()) {
619        headers.add(h.toString());
620      }
621      logger.logResponse(response.getStatusLine().toString(), headers, cnt);
622    }
623    return cnt;
624  }
625
626  public ToolingClientLogger getLogger() {
627    return logger;
628  }
629
630  public void setLogger(ToolingClientLogger logger) {
631    this.logger = logger;
632  }
633
634  
635  /**
636   * Used for debugging
637   * 
638   * @param instream
639   * @return
640   */
641  protected String writeInputStreamAsString(InputStream instream) {
642    String value = null;
643    try {
644      value = IOUtils.toString(instream, "UTF-8");
645      System.out.println(value);
646
647    } catch(IOException ioe) {
648      //Do nothing
649    }
650    return value;
651  }
652
653
654}