Inclusion of jaxrs-client : complete removal of the dependencies for jaxrsserver project to apache httpclient and httpcore and only rely on jax-rs
This commit is contained in:
parent
f8ea639af9
commit
d64cf57b0d
|
@ -38,8 +38,6 @@ import java.util.Set;
|
|||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.Validate;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpEntityEnclosingRequest;
|
||||
import org.hl7.fhir.instance.model.api.IBase;
|
||||
import org.hl7.fhir.instance.model.api.IBaseOperationOutcome;
|
||||
import org.hl7.fhir.instance.model.api.IBaseResource;
|
||||
|
@ -222,12 +220,9 @@ public abstract class BaseClient implements IRestfulClient {
|
|||
|
||||
if (theLogRequestAndResponse) {
|
||||
ourLog.info("Client invoking: {}", httpRequest);
|
||||
if (httpRequest instanceof HttpEntityEnclosingRequest) {
|
||||
HttpEntity entity = ((HttpEntityEnclosingRequest) httpRequest).getEntity();
|
||||
if (entity.isRepeatable()) {
|
||||
String content = IOUtils.toString(entity.getContent());
|
||||
ourLog.info("Client request body: {}", content);
|
||||
}
|
||||
String body = httpRequest.getRequestBodyFromStream();
|
||||
if(body != null) {
|
||||
ourLog.info("Client request body: {}", body);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -27,11 +27,9 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
|
||||
import ca.uhn.fhir.context.FhirContext;
|
||||
import ca.uhn.fhir.rest.api.RequestTypeEnum;
|
||||
import ca.uhn.fhir.rest.client.api.Header;
|
||||
import ca.uhn.fhir.rest.client.api.IHttpClient;
|
||||
import ca.uhn.fhir.rest.client.api.IHttpRequest;
|
||||
import ca.uhn.fhir.rest.server.EncodingEnum;
|
||||
|
@ -47,7 +45,7 @@ public abstract class BaseHttpClientInvocation {
|
|||
}
|
||||
|
||||
public void addHeader(String theName, String theValue) {
|
||||
myHeaders.add(new BasicHeader(theName, theValue));
|
||||
myHeaders.add(new Header(theName, theValue));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -23,10 +23,9 @@ package ca.uhn.fhir.rest.client;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.http.Header;
|
||||
|
||||
import ca.uhn.fhir.context.ConfigurationException;
|
||||
import ca.uhn.fhir.rest.api.RequestTypeEnum;
|
||||
import ca.uhn.fhir.rest.client.api.Header;
|
||||
import ca.uhn.fhir.rest.client.api.IHttpClient;
|
||||
import ca.uhn.fhir.rest.client.api.IRestfulClient;
|
||||
|
||||
|
|
|
@ -26,7 +26,6 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.HttpClient;
|
||||
|
@ -43,6 +42,7 @@ import org.hl7.fhir.instance.model.api.IBaseBinary;
|
|||
|
||||
import ca.uhn.fhir.rest.api.RequestTypeEnum;
|
||||
import ca.uhn.fhir.rest.client.BaseHttpClientInvocation;
|
||||
import ca.uhn.fhir.rest.client.api.Header;
|
||||
import ca.uhn.fhir.rest.client.api.IHttpClient;
|
||||
import ca.uhn.fhir.rest.client.api.IHttpRequest;
|
||||
import ca.uhn.fhir.rest.server.Constants;
|
||||
|
|
|
@ -26,7 +26,10 @@ import java.util.LinkedList;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpEntityEnclosingRequest;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpRequestBase;
|
||||
|
||||
|
@ -84,4 +87,15 @@ public class ApacheHttpRequest implements IHttpRequest {
|
|||
return myRequest.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRequestBodyFromStream() throws IOException {
|
||||
if (myRequest instanceof HttpEntityEnclosingRequest) {
|
||||
HttpEntity entity = ((HttpEntityEnclosingRequest) myRequest).getEntity();
|
||||
if (entity.isRepeatable()) {
|
||||
return IOUtils.toString(entity.getContent());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -25,7 +25,6 @@ import java.util.Map;
|
|||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
|
@ -41,6 +40,7 @@ import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
|||
import ca.uhn.fhir.context.FhirContext;
|
||||
import ca.uhn.fhir.rest.api.RequestTypeEnum;
|
||||
import ca.uhn.fhir.rest.client.RestfulClientFactory;
|
||||
import ca.uhn.fhir.rest.client.api.Header;
|
||||
import ca.uhn.fhir.rest.client.api.IHttpClient;
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
package ca.uhn.fhir.rest.client.api;
|
||||
|
||||
/*
|
||||
* #%L
|
||||
* HAPI FHIR - Core Library
|
||||
* %%
|
||||
* Copyright (C) 2014 - 2016 University Health Network
|
||||
* %%
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
/**
|
||||
* Represents an HTTP header field.
|
||||
*/
|
||||
public class Header {
|
||||
|
||||
public final String myName;
|
||||
public final String myValue;
|
||||
|
||||
public Header(String myName, String myValue) {
|
||||
this.myName = myName;
|
||||
this.myValue = myValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the Header.
|
||||
*
|
||||
* @return the name of the Header, never {@code null}
|
||||
*/
|
||||
public String getName() {
|
||||
return myName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of the Header.
|
||||
*
|
||||
* @return the value of the Header, may be {@code null}
|
||||
*/
|
||||
public String getValue() {
|
||||
return myValue;
|
||||
}
|
||||
|
||||
}
|
|
@ -48,4 +48,12 @@ public interface IHttpRequest {
|
|||
*/
|
||||
public Map<String, List<String>> getAllHeaders();
|
||||
|
||||
/**
|
||||
* Return the requestbody as a string.
|
||||
* If this is not supported by the underlying technology, null is returned
|
||||
* @return a string representation of the request or null if not supported or empty.
|
||||
* @throws IOException
|
||||
*/
|
||||
public String getRequestBodyFromStream() throws IOException;
|
||||
|
||||
}
|
||||
|
|
|
@ -28,8 +28,6 @@ import java.util.Map;
|
|||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang3.Validate;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpEntityEnclosingRequest;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import ca.uhn.fhir.rest.client.IClientInterceptor;
|
||||
|
@ -84,21 +82,17 @@ public class LoggingInterceptor implements IClientInterceptor {
|
|||
}
|
||||
|
||||
if (myLogRequestBody) {
|
||||
if (theRequest instanceof HttpEntityEnclosingRequest) {
|
||||
HttpEntity entity = ((HttpEntityEnclosingRequest) theRequest).getEntity();
|
||||
if (entity.isRepeatable()) {
|
||||
try {
|
||||
String content = IOUtils.toString(entity.getContent());
|
||||
myLog.info("Client request body:\n{}", content);
|
||||
} catch (IllegalStateException e) {
|
||||
myLog.warn("Failed to replay request contents (during logging attempt, actual FHIR call did not fail)", e);
|
||||
} catch (IOException e) {
|
||||
myLog.warn("Failed to replay request contents (during logging attempt, actual FHIR call did not fail)", e);
|
||||
}
|
||||
try {
|
||||
String content = theRequest.getRequestBodyFromStream();
|
||||
if (content != null) {
|
||||
myLog.info("Client request body:\n{}", content);
|
||||
}
|
||||
} catch (IllegalStateException e) {
|
||||
myLog.warn("Failed to replay request contents (during logging attempt, actual FHIR call did not fail)", e);
|
||||
} catch (IOException e) {
|
||||
myLog.warn("Failed to replay request contents (during logging attempt, actual FHIR call did not fail)", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -127,17 +121,22 @@ public class LoggingInterceptor implements IClientInterceptor {
|
|||
|
||||
if (myLogResponseBody) {
|
||||
theResponse.bufferEntitity();
|
||||
InputStream respEntity = theResponse.readEntity();
|
||||
if (respEntity != null) {
|
||||
final byte[] bytes;
|
||||
try {
|
||||
bytes = IOUtils.toByteArray(respEntity);
|
||||
} catch (IllegalStateException e) {
|
||||
throw new InternalErrorException(e);
|
||||
InputStream respEntity = null;
|
||||
try {
|
||||
respEntity = theResponse.readEntity();
|
||||
if (respEntity != null) {
|
||||
final byte[] bytes;
|
||||
try {
|
||||
bytes = IOUtils.toByteArray(respEntity);
|
||||
} catch (IllegalStateException e) {
|
||||
throw new InternalErrorException(e);
|
||||
}
|
||||
myLog.info("Client response body:\n{}", new String(bytes, "UTF-8"));
|
||||
} else {
|
||||
myLog.info("Client response body: (none)");
|
||||
}
|
||||
myLog.info("Client response body:\n{}", new String(bytes, "UTF-8"));
|
||||
} else {
|
||||
myLog.info("Client response body: (none)");
|
||||
} finally {
|
||||
IOUtils.closeQuietly(respEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,9 +23,6 @@ package ca.uhn.fhir.rest.method;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpRequestBase;
|
||||
|
||||
import ca.uhn.fhir.context.FhirContext;
|
||||
import ca.uhn.fhir.rest.api.RequestTypeEnum;
|
||||
import ca.uhn.fhir.rest.client.BaseHttpClientInvocation;
|
||||
|
|
|
@ -24,7 +24,6 @@ import javax.servlet.http.HttpServletRequest;
|
|||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.client.utils.DateUtils;
|
||||
import org.hl7.fhir.instance.model.api.IAnyResource;
|
||||
import org.hl7.fhir.instance.model.api.IBaseMetaType;
|
||||
import org.hl7.fhir.instance.model.api.IBaseResource;
|
||||
|
@ -89,6 +88,7 @@ import ca.uhn.fhir.rest.server.Constants;
|
|||
import ca.uhn.fhir.rest.server.EncodingEnum;
|
||||
import ca.uhn.fhir.rest.server.IDynamicSearchResourceProvider;
|
||||
import ca.uhn.fhir.rest.server.SearchParameterMap;
|
||||
import ca.uhn.fhir.util.DateUtils;
|
||||
import ca.uhn.fhir.util.ReflectionUtil;
|
||||
|
||||
/*
|
||||
|
|
|
@ -4,6 +4,7 @@ import java.io.IOException;
|
|||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/*
|
||||
* #%L
|
||||
|
@ -274,4 +275,9 @@ public abstract class RequestDetails {
|
|||
mySecondaryOperation = theSecondaryOperation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the charset as defined by the header contenttype. Return null if it is not set.
|
||||
*/
|
||||
public abstract Charset getCharset();
|
||||
|
||||
}
|
||||
|
|
|
@ -20,7 +20,6 @@ package ca.uhn.fhir.rest.param;
|
|||
* #L%
|
||||
*/
|
||||
import static org.apache.commons.lang3.StringUtils.isBlank;
|
||||
import static org.apache.commons.lang3.StringUtils.isNotBlank;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
|
@ -35,7 +34,6 @@ import java.util.Map;
|
|||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang3.Validate;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.hl7.fhir.instance.model.api.IBaseBinary;
|
||||
import org.hl7.fhir.instance.model.api.IBaseResource;
|
||||
|
||||
|
@ -128,13 +126,7 @@ public class ResourceParameter implements IParameter {
|
|||
}
|
||||
|
||||
public static Charset determineRequestCharset(RequestDetails theRequest) {
|
||||
String ct = theRequest.getHeader(Constants.HEADER_CONTENT_TYPE);
|
||||
|
||||
Charset charset = null;
|
||||
if (isNotBlank(ct)) {
|
||||
ContentType parsedCt = ContentType.parse(ct);
|
||||
charset = parsedCt.getCharset();
|
||||
}
|
||||
Charset charset = theRequest.getCharset();
|
||||
if (charset == null) {
|
||||
charset = Charset.forName("UTF-8");
|
||||
}
|
||||
|
|
|
@ -43,7 +43,6 @@ import java.util.regex.Pattern;
|
|||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.client.utils.DateUtils;
|
||||
import org.hl7.fhir.instance.model.api.IAnyResource;
|
||||
import org.hl7.fhir.instance.model.api.IBaseBinary;
|
||||
import org.hl7.fhir.instance.model.api.IBaseResource;
|
||||
|
@ -69,6 +68,7 @@ import ca.uhn.fhir.rest.method.RequestDetails;
|
|||
import ca.uhn.fhir.rest.method.SummaryEnumParameter;
|
||||
import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
|
||||
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
|
||||
import ca.uhn.fhir.util.DateUtils;
|
||||
|
||||
public class RestfulServerUtils {
|
||||
static final Pattern ACCEPT_HEADER_PATTERN = Pattern.compile("\\s*([a-zA-Z0-9+.*/-]+)\\s*(;\\s*([a-zA-Z]+)\\s*=\\s*([a-zA-Z0-9.]+)\\s*)?(,?)");
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package ca.uhn.fhir.rest.server.servlet;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
||||
/*
|
||||
* #%L
|
||||
* HAPI FHIR - Core Library
|
||||
|
@ -21,10 +19,13 @@ import java.io.ByteArrayInputStream;
|
|||
* limitations under the License.
|
||||
* #L%
|
||||
*/
|
||||
import static org.apache.commons.lang3.StringUtils.isNotBlank;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
|
@ -37,6 +38,7 @@ import javax.servlet.http.HttpServletRequest;
|
|||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.http.entity.ContentType;
|
||||
|
||||
import ca.uhn.fhir.context.ConfigurationException;
|
||||
import ca.uhn.fhir.rest.api.RequestTypeEnum;
|
||||
|
@ -176,4 +178,16 @@ public class ServletRequestDetails extends RequestDetails {
|
|||
return retVal;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Charset getCharset() {
|
||||
String ct = getHeader(Constants.HEADER_CONTENT_TYPE);
|
||||
|
||||
Charset charset = null;
|
||||
if (isNotBlank(ct)) {
|
||||
ContentType parsedCt = ContentType.parse(ct);
|
||||
charset = parsedCt.getCharset();
|
||||
}
|
||||
return charset;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,254 @@
|
|||
package ca.uhn.fhir.util;
|
||||
|
||||
/*
|
||||
* ====================================================================
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.text.ParsePosition;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
* A utility class for parsing and formatting HTTP dates as used in cookies and
|
||||
* other headers. This class handles dates as defined by RFC 2616 section
|
||||
* 3.3.1 as well as some other common non-standard formats.
|
||||
*
|
||||
* @since 4.3
|
||||
*/
|
||||
public final class DateUtils {
|
||||
|
||||
/**
|
||||
* Date format pattern used to parse HTTP date headers in RFC 1123 format.
|
||||
*/
|
||||
public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";
|
||||
|
||||
/**
|
||||
* Date format pattern used to parse HTTP date headers in RFC 1036 format.
|
||||
*/
|
||||
public static final String PATTERN_RFC1036 = "EEE, dd-MMM-yy HH:mm:ss zzz";
|
||||
|
||||
/**
|
||||
* Date format pattern used to parse HTTP date headers in ANSI C
|
||||
* {@code asctime()} format.
|
||||
*/
|
||||
public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy";
|
||||
|
||||
private static final String[] DEFAULT_PATTERNS = new String[] {
|
||||
PATTERN_RFC1123,
|
||||
PATTERN_RFC1036,
|
||||
PATTERN_ASCTIME
|
||||
};
|
||||
|
||||
private static final Date DEFAULT_TWO_DIGIT_YEAR_START;
|
||||
|
||||
public static final TimeZone GMT = TimeZone.getTimeZone("GMT");
|
||||
|
||||
static {
|
||||
final Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTimeZone(GMT);
|
||||
calendar.set(2000, Calendar.JANUARY, 1, 0, 0, 0);
|
||||
calendar.set(Calendar.MILLISECOND, 0);
|
||||
DEFAULT_TWO_DIGIT_YEAR_START = calendar.getTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a date value. The formats used for parsing the date value are retrieved from
|
||||
* the default http params.
|
||||
*
|
||||
* @param dateValue the date value to parse
|
||||
*
|
||||
* @return the parsed date or null if input could not be parsed
|
||||
*/
|
||||
public static Date parseDate(final String dateValue) {
|
||||
return parseDate(dateValue, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the date value using the given date formats.
|
||||
*
|
||||
* @param dateValue the date value to parse
|
||||
* @param dateFormats the date formats to use
|
||||
*
|
||||
* @return the parsed date or null if input could not be parsed
|
||||
*/
|
||||
public static Date parseDate(final String dateValue, final String[] dateFormats) {
|
||||
return parseDate(dateValue, dateFormats, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the date value using the given date formats.
|
||||
*
|
||||
* @param dateValue the date value to parse
|
||||
* @param dateFormats the date formats to use
|
||||
* @param startDate During parsing, two digit years will be placed in the range
|
||||
* {@code startDate} to {@code startDate + 100 years}. This value may
|
||||
* be {@code null}. When {@code null} is given as a parameter, year
|
||||
* {@code 2000} will be used.
|
||||
*
|
||||
* @return the parsed date or null if input could not be parsed
|
||||
*/
|
||||
public static Date parseDate(
|
||||
final String dateValue,
|
||||
final String[] dateFormats,
|
||||
final Date startDate) {
|
||||
notNull(dateValue, "Date value");
|
||||
final String[] localDateFormats = dateFormats != null ? dateFormats : DEFAULT_PATTERNS;
|
||||
final Date localStartDate = startDate != null ? startDate : DEFAULT_TWO_DIGIT_YEAR_START;
|
||||
String v = dateValue;
|
||||
// trim single quotes around date if present
|
||||
// see issue #5279
|
||||
if (v.length() > 1 && v.startsWith("'") && v.endsWith("'")) {
|
||||
v = v.substring (1, v.length() - 1);
|
||||
}
|
||||
|
||||
for (final String dateFormat : localDateFormats) {
|
||||
final SimpleDateFormat dateParser = DateFormatHolder.formatFor(dateFormat);
|
||||
dateParser.set2DigitYearStart(localStartDate);
|
||||
final ParsePosition pos = new ParsePosition(0);
|
||||
final Date result = dateParser.parse(v, pos);
|
||||
if (pos.getIndex() != 0) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the given date according to the RFC 1123 pattern.
|
||||
*
|
||||
* @param date The date to format.
|
||||
* @return An RFC 1123 formatted date string.
|
||||
*
|
||||
* @see #PATTERN_RFC1123
|
||||
*/
|
||||
public static String formatDate(final Date date) {
|
||||
return formatDate(date, PATTERN_RFC1123);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the given date according to the specified pattern. The pattern
|
||||
* must conform to that used by the {@link SimpleDateFormat simple date
|
||||
* format} class.
|
||||
*
|
||||
* @param date The date to format.
|
||||
* @param pattern The pattern to use for formatting the date.
|
||||
* @return A formatted date string.
|
||||
*
|
||||
* @throws IllegalArgumentException If the given date pattern is invalid.
|
||||
*
|
||||
* @see SimpleDateFormat
|
||||
*/
|
||||
public static String formatDate(final Date date, final String pattern) {
|
||||
notNull(date, "Date");
|
||||
notNull(pattern, "Pattern");
|
||||
final SimpleDateFormat formatter = DateFormatHolder.formatFor(pattern);
|
||||
return formatter.format(date);
|
||||
}
|
||||
|
||||
|
||||
public static <T> T notNull(final T argument, final String name) {
|
||||
if (argument == null) {
|
||||
throw new IllegalArgumentException(name + " may not be null");
|
||||
}
|
||||
return argument;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears thread-local variable containing {@link java.text.DateFormat} cache.
|
||||
*
|
||||
* @since 4.3
|
||||
*/
|
||||
public static void clearThreadLocal() {
|
||||
DateFormatHolder.clearThreadLocal();
|
||||
}
|
||||
|
||||
/** This class should not be instantiated. */
|
||||
private DateUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for {@link SimpleDateFormat}s. The instances are stored in a
|
||||
* threadlocal way because SimpleDateFormat is not threadsafe as noted in
|
||||
* {@link SimpleDateFormat its javadoc}.
|
||||
*
|
||||
*/
|
||||
final static class DateFormatHolder {
|
||||
|
||||
private static final ThreadLocal<SoftReference<Map<String, SimpleDateFormat>>>
|
||||
THREADLOCAL_FORMATS = new ThreadLocal<SoftReference<Map<String, SimpleDateFormat>>>() {
|
||||
|
||||
@Override
|
||||
protected SoftReference<Map<String, SimpleDateFormat>> initialValue() {
|
||||
return new SoftReference<Map<String, SimpleDateFormat>>(
|
||||
new HashMap<String, SimpleDateFormat>());
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* creates a {@link SimpleDateFormat} for the requested format string.
|
||||
*
|
||||
* @param pattern
|
||||
* a non-{@code null} format String according to
|
||||
* {@link SimpleDateFormat}. The format is not checked against
|
||||
* {@code null} since all paths go through
|
||||
* {@link DateUtils}.
|
||||
* @return the requested format. This simple dateformat should not be used
|
||||
* to {@link SimpleDateFormat#applyPattern(String) apply} to a
|
||||
* different pattern.
|
||||
*/
|
||||
public static SimpleDateFormat formatFor(final String pattern) {
|
||||
final SoftReference<Map<String, SimpleDateFormat>> ref = THREADLOCAL_FORMATS.get();
|
||||
Map<String, SimpleDateFormat> formats = ref.get();
|
||||
if (formats == null) {
|
||||
formats = new HashMap<String, SimpleDateFormat>();
|
||||
THREADLOCAL_FORMATS.set(
|
||||
new SoftReference<Map<String, SimpleDateFormat>>(formats));
|
||||
}
|
||||
|
||||
SimpleDateFormat format = formats.get(pattern);
|
||||
if (format == null) {
|
||||
format = new SimpleDateFormat(pattern, Locale.US);
|
||||
format.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
formats.put(pattern, format);
|
||||
}
|
||||
|
||||
return format;
|
||||
}
|
||||
|
||||
public static void clearThreadLocal() {
|
||||
THREADLOCAL_FORMATS.remove();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -24,6 +24,14 @@
|
|||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpcore</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<!-- conformance profile -->
|
||||
|
|
|
@ -30,11 +30,11 @@ import javax.ws.rs.core.Form;
|
|||
import javax.ws.rs.core.MultivaluedHashMap;
|
||||
import javax.ws.rs.core.MultivaluedMap;
|
||||
|
||||
import org.apache.http.Header;
|
||||
import org.hl7.fhir.instance.model.api.IBaseBinary;
|
||||
|
||||
import ca.uhn.fhir.rest.api.RequestTypeEnum;
|
||||
import ca.uhn.fhir.rest.client.BaseHttpClientInvocation;
|
||||
import ca.uhn.fhir.rest.client.api.Header;
|
||||
import ca.uhn.fhir.rest.client.api.IHttpClient;
|
||||
import ca.uhn.fhir.rest.client.api.IHttpRequest;
|
||||
import ca.uhn.fhir.rest.server.Constants;
|
||||
|
|
|
@ -27,6 +27,7 @@ import java.util.Map;
|
|||
|
||||
import javax.ws.rs.client.Entity;
|
||||
import javax.ws.rs.client.Invocation;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
import ca.uhn.fhir.rest.api.RequestTypeEnum;
|
||||
import ca.uhn.fhir.rest.client.api.IHttpRequest;
|
||||
|
@ -86,7 +87,9 @@ public class JaxRsHttpRequest implements IHttpRequest {
|
|||
|
||||
@Override
|
||||
public IHttpResponse execute() {
|
||||
return new JaxRsHttpResponse(getRequest().build(getRequestType().name(), getEntity()).invoke());
|
||||
Invocation invocation = getRequest().build(getRequestType().name(), getEntity());
|
||||
Response response = invocation.invoke();
|
||||
return new JaxRsHttpResponse(response);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -94,4 +97,10 @@ public class JaxRsHttpRequest implements IHttpRequest {
|
|||
return this.myHeaders;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRequestBodyFromStream() {
|
||||
// not supported
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -28,11 +28,9 @@ import java.util.Map;
|
|||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import javax.ws.rs.core.HttpHeaders;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
import org.apache.http.entity.ContentType;
|
||||
|
||||
import ca.uhn.fhir.rest.client.api.IHttpResponse;
|
||||
|
||||
/**
|
||||
|
@ -60,8 +58,9 @@ public class JaxRsHttpResponse implements IHttpResponse {
|
|||
|
||||
@Override
|
||||
public String getMimeType() {
|
||||
ContentType ct = ContentType.parse(myResponse.getHeaderString(HttpHeaders.CONTENT_TYPE));
|
||||
return ct != null ? ct.getMimeType() : null;
|
||||
MediaType mediaType = myResponse.getMediaType();
|
||||
//Keep only type and subtype and do not include the parameters such as charset
|
||||
return new MediaType(mediaType.getType(), mediaType.getSubtype()).toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -94,7 +93,7 @@ public class JaxRsHttpResponse implements IHttpResponse {
|
|||
|
||||
@Override
|
||||
public void bufferEntitity() {
|
||||
if(myResponse.hasEntity()) {
|
||||
if(!myBufferedEntity && myResponse.hasEntity()) {
|
||||
myBufferedEntity = true;
|
||||
myResponse.bufferEntity();
|
||||
} else {
|
||||
|
|
|
@ -23,10 +23,9 @@ package ca.uhn.fhir.jaxrs.client;
|
|||
import ca.uhn.fhir.context.FhirContext;
|
||||
import ca.uhn.fhir.rest.api.RequestTypeEnum;
|
||||
import ca.uhn.fhir.rest.client.RestfulClientFactory;
|
||||
import ca.uhn.fhir.rest.client.api.Header;
|
||||
import ca.uhn.fhir.rest.client.api.IHttpClient;
|
||||
|
||||
import org.apache.http.Header;
|
||||
|
||||
import javax.ws.rs.client.Client;
|
||||
import javax.ws.rs.client.ClientBuilder;
|
||||
import java.util.List;
|
||||
|
|
|
@ -23,10 +23,12 @@ package ca.uhn.fhir.jaxrs.server.util;
|
|||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.ws.rs.core.HttpHeaders;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
|
@ -149,8 +151,8 @@ public class JaxRsRequest extends RequestDetails {
|
|||
}
|
||||
}
|
||||
|
||||
private String theResourceString;
|
||||
private HttpHeaders headers;
|
||||
private String myResourceString;
|
||||
private HttpHeaders myHeaders;
|
||||
private AbstractJaxRsProvider myServer;
|
||||
|
||||
/**
|
||||
|
@ -162,8 +164,8 @@ public class JaxRsRequest extends RequestDetails {
|
|||
*/
|
||||
public JaxRsRequest(AbstractJaxRsProvider server, String resourceString, RequestTypeEnum requestType,
|
||||
RestOperationTypeEnum restOperation) {
|
||||
this.headers = server.getHeaders();
|
||||
this.theResourceString = resourceString;
|
||||
this.myHeaders = server.getHeaders();
|
||||
this.myResourceString = resourceString;
|
||||
this.setRestOperationType(restOperation);
|
||||
setServer(server);
|
||||
setFhirServerBase(server.getBaseForServer());
|
||||
|
@ -192,7 +194,7 @@ public class JaxRsRequest extends RequestDetails {
|
|||
|
||||
@Override
|
||||
public List<String> getHeaders(String name) {
|
||||
List<String> requestHeader = headers.getRequestHeader(name);
|
||||
List<String> requestHeader = myHeaders.getRequestHeader(name);
|
||||
return requestHeader == null ? Collections.<String> emptyList() : requestHeader;
|
||||
}
|
||||
|
||||
|
@ -203,7 +205,7 @@ public class JaxRsRequest extends RequestDetails {
|
|||
|
||||
@Override
|
||||
protected byte[] getByteStreamRequestContents() {
|
||||
return StringUtils.defaultIfEmpty(theResourceString, "")
|
||||
return StringUtils.defaultIfEmpty(myResourceString, "")
|
||||
.getBytes(ResourceParameter.determineRequestCharset(this));
|
||||
}
|
||||
|
||||
|
@ -226,4 +228,18 @@ public class JaxRsRequest extends RequestDetails {
|
|||
// not yet implemented
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Charset getCharset() {
|
||||
String charset = null;
|
||||
|
||||
if(myHeaders.getMediaType() != null && myHeaders.getMediaType().getParameters() != null) {
|
||||
charset = myHeaders.getMediaType().getParameters().get(MediaType.CHARSET_PARAMETER);
|
||||
}
|
||||
if(charset != null) {
|
||||
return Charset.forName(charset);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -105,9 +105,9 @@ public class AbstractJaxRsResourceProviderTest {
|
|||
|
||||
jettyServer.start();
|
||||
|
||||
ourCtx.setRestfulClientFactory(new JaxRsRestfulClientFactory(ourCtx));
|
||||
ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER);
|
||||
ourCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000);
|
||||
ourCtx.setRestfulClientFactory(new JaxRsRestfulClientFactory(ourCtx));
|
||||
serverBase = "http://localhost:" + ourPort + "/";
|
||||
client = ourCtx.newRestfulGenericClient(serverBase);
|
||||
client.setEncoding(EncodingEnum.JSON);
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package ca.uhn.fhir.jaxrs.server.example;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
|
|
|
@ -18,6 +18,7 @@ import org.junit.Ignore;
|
|||
import org.junit.Test;
|
||||
|
||||
import ca.uhn.fhir.context.FhirContext;
|
||||
import ca.uhn.fhir.jaxrs.client.JaxRsRestfulClientFactory;
|
||||
import ca.uhn.fhir.model.api.BundleEntry;
|
||||
import ca.uhn.fhir.model.dstu2.composite.HumanNameDt;
|
||||
import ca.uhn.fhir.model.dstu2.resource.Bundle;
|
||||
|
@ -66,6 +67,7 @@ public class JaxRsPatientProviderTest {
|
|||
//@formatter:on
|
||||
jettyServer.start();
|
||||
|
||||
ourCtx.setRestfulClientFactory(new JaxRsRestfulClientFactory(ourCtx));
|
||||
ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER);
|
||||
ourCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000);
|
||||
client = ourCtx.newRestfulGenericClient("http://localhost:" + ourPort + "/");
|
||||
|
|
Loading…
Reference in New Issue