Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit be9bcf5

Browse files
committedJun 18, 2019
Portability to Java 6 support
This framework should be able to be binary compatible with Java 6 as Netty does. This merge request allows minor changes to be able to be compatible. Also it upgrades dependencies.
1 parent 02fdbb4 commit be9bcf5

22 files changed

+307
-124
lines changed
 

‎pom.xml

+6-7
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@
6262
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
6363
<surefire.redirectTestOutputToFile>true</surefire.redirectTestOutputToFile>
6464

65-
<rs-api.version>2.0</rs-api.version>
66-
<netty.version>4.1.16.Final</netty.version>
67-
<slf4j.version>1.7.5</slf4j.version>
65+
<rs-api.version>2.1.1</rs-api.version>
66+
<netty.version>4.1.36.Final</netty.version>
67+
<slf4j.version>1.7.26</slf4j.version>
6868
<findbugs.version>2.0.1</findbugs.version>
6969
<beanutils.version>1.8.3</beanutils.version>
70-
<junit.version>4.11</junit.version>
71-
<logback.version>1.0.9</logback.version>
72-
<gson.version>2.2.4</gson.version>
70+
<junit.version>4.12</junit.version>
71+
<logback.version>1.2.3</logback.version>
72+
<gson.version>2.8.5</gson.version>
7373
</properties>
7474

7575
<dependencies>
@@ -174,7 +174,6 @@
174174
</execution>
175175
</executions>
176176
</plugin>
177-
178177
<!-- Checkstyle -->
179178
<plugin>
180179
<groupId>org.apache.maven.plugins</groupId>

‎src/main/java/io/cdap/http/AbstractHttpResponder.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import java.io.File;
2828
import java.io.IOException;
2929
import java.nio.ByteBuffer;
30-
import java.nio.charset.StandardCharsets;
30+
import java.nio.charset.Charset;
3131

3232
/**
3333
* Base implementation of {@link HttpResponder} to simplify child implementations.
@@ -53,7 +53,7 @@ public void sendString(HttpResponseStatus status, String data, HttpHeaders heade
5353
sendStatus(status, headers);
5454
return;
5555
}
56-
ByteBuf buffer = Unpooled.wrappedBuffer(StandardCharsets.UTF_8.encode(data));
56+
ByteBuf buffer = Unpooled.wrappedBuffer(Charset.forName("UTF-8").encode(data));
5757
sendContent(status, buffer, addContentTypeIfMissing(new DefaultHttpHeaders().add(headers),
5858
"text/plain; charset=utf-8"));
5959
}

‎src/main/java/io/cdap/http/NettyHttpService.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ private NettyHttpService(String serviceName,
129129
this.workerThreadPoolSize = workerThreadPoolSize;
130130
this.execThreadPoolSize = execThreadPoolSize;
131131
this.execThreadKeepAliveSecs = execThreadKeepAliveSecs;
132-
this.channelConfigs = new HashMap<>(channelConfigs);
133-
this.childChannelConfigs = new HashMap<>(childChannelConfigs);
132+
this.channelConfigs = new HashMap<ChannelOption, Object>(channelConfigs);
133+
this.childChannelConfigs = new HashMap<ChannelOption, Object>(childChannelConfigs);
134134
this.rejectedExecutionHandler = rejectedExecutionHandler;
135135
this.resourceHandler = new HttpResourceHandler(httpHandlers, handlerHooks, urlRewriter, exceptionHandler);
136136
this.handlerContext = new BasicHandlerContext(this.resourceHandler);
@@ -193,10 +193,9 @@ public synchronized void start() throws Exception {
193193
shutdownExecutorGroups(0, 5, TimeUnit.SECONDS, eventExecutorGroup);
194194
}
195195
} catch (Throwable t2) {
196-
t.addSuppressed(t2);
197196
}
198197
state = State.FAILED;
199-
throw t;
198+
throw (Exception) t;
200199
}
201200
}
202201

@@ -255,7 +254,7 @@ public synchronized void stop(long quietPeriod, long timeout, TimeUnit unit) thr
255254
}
256255
} catch (Throwable t) {
257256
state = State.FAILED;
258-
throw t;
257+
throw (Exception) t;
259258
}
260259
state = State.STOPPED;
261260
LOG.debug("Stopped HTTP Service {} on address {}", serviceName, bindAddress);
@@ -370,7 +369,7 @@ protected void initChannel(SocketChannel ch) throws Exception {
370369
*/
371370
private void shutdownExecutorGroups(long quietPeriod, long timeout, TimeUnit unit, EventExecutorGroup...groups) {
372371
Exception ex = null;
373-
List<Future<?>> futures = new ArrayList<>();
372+
List<Future<?>> futures = new ArrayList<Future<?>>();
374373
for (EventExecutorGroup group : groups) {
375374
if (group == null) {
376375
continue;
@@ -385,7 +384,6 @@ private void shutdownExecutorGroups(long quietPeriod, long timeout, TimeUnit uni
385384
if (ex == null) {
386385
ex = e;
387386
} else {
388-
ex.addSuppressed(e);
389387
}
390388
}
391389
}
@@ -442,8 +440,8 @@ protected Builder(String serviceName) {
442440
rejectedExecutionHandler = DEFAULT_REJECTED_EXECUTION_HANDLER;
443441
httpChunkLimit = DEFAULT_HTTP_CHUNK_LIMIT;
444442
port = 0;
445-
channelConfigs = new HashMap<>();
446-
childChannelConfigs = new HashMap<>();
443+
channelConfigs = new HashMap<ChannelOption, Object>();
444+
childChannelConfigs = new HashMap<ChannelOption, Object>();
447445
channelConfigs.put(ChannelOption.SO_BACKLOG, DEFAULT_CONNECTION_BACKLOG);
448446
sslHandlerFactory = null;
449447
exceptionHandler = new ExceptionHandler();

‎src/main/java/io/cdap/http/SSLHandlerFactory.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,17 @@ public SSLHandlerFactory(SslContext sslContext) {
7070
}
7171

7272
private static KeyStore getKeyStore(File keyStore, String keyStorePassword) throws Exception {
73-
try (InputStream is = new FileInputStream(keyStore)) {
73+
InputStream is = null;
74+
try {
75+
is = new FileInputStream(keyStore);
7476
KeyStore ks = KeyStore.getInstance("JKS");
7577
ks.load(is, keyStorePassword.toCharArray());
78+
is.close();
7679
return ks;
80+
} finally {
81+
if (is != null) {
82+
is.close();
83+
}
7784
}
7885
}
7986

‎src/main/java/io/cdap/http/internal/BasicHttpResponder.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
import java.io.File;
5151
import java.io.IOException;
5252
import java.io.RandomAccessFile;
53-
import java.nio.charset.StandardCharsets;
53+
import java.nio.charset.Charset;
5454
import java.util.concurrent.atomic.AtomicBoolean;
5555
import javax.annotation.Nullable;
5656

@@ -136,9 +136,8 @@ public void sendFile(File file, HttpHeaders headers) throws IOException {
136136
try {
137137
raf.close();
138138
} catch (IOException ex) {
139-
t.addSuppressed(ex);
140139
}
141-
throw t;
140+
throw (IOException) t;
142141
}
143142
}
144143

@@ -152,7 +151,8 @@ public void sendContent(HttpResponseStatus status, final BodyProducer bodyProduc
152151
// Response with error and close the connection
153152
sendContent(
154153
HttpResponseStatus.INTERNAL_SERVER_ERROR,
155-
Unpooled.copiedBuffer("Failed to determined content length. Cause: " + t.getMessage(), StandardCharsets.UTF_8),
154+
Unpooled.copiedBuffer("Failed to determined content length. Cause: " + t.getMessage(),
155+
Charset.forName("UTF-8")),
156156
new DefaultHttpHeaders()
157157
.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE)
158158
.set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=utf-8"));
@@ -197,7 +197,7 @@ boolean isResponded() {
197197
private ChannelFutureListener createBodyProducerCompletionListener(final BodyProducer bodyProducer) {
198198
return new ChannelFutureListener() {
199199
@Override
200-
public void operationComplete(ChannelFuture future) throws Exception {
200+
public void operationComplete(ChannelFuture future) {
201201
if (!future.isSuccess()) {
202202
callBodyProducerHandleError(bodyProducer, future.cause());
203203
channel.close();

‎src/main/java/io/cdap/http/internal/HandlerException.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import io.netty.handler.codec.http.HttpUtil;
2525
import io.netty.handler.codec.http.HttpVersion;
2626

27-
import java.nio.charset.StandardCharsets;
27+
import java.nio.charset.Charset;
2828

2929
/**
3030
*Creating Http Response for Exception messages.
@@ -48,7 +48,7 @@ final class HandlerException extends Exception {
4848

4949
HttpResponse createFailureResponse() {
5050
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, failureStatus,
51-
Unpooled.copiedBuffer(message, StandardCharsets.UTF_8));
51+
Unpooled.copiedBuffer(message, Charset.forName("UTF-8")));
5252
HttpUtil.setContentLength(response, response.content().readableBytes());
5353
return response;
5454
}

‎src/main/java/io/cdap/http/internal/HttpResourceHandler.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import javax.annotation.Nullable;
4545
import javax.ws.rs.DELETE;
4646
import javax.ws.rs.GET;
47+
import javax.ws.rs.OPTIONS;
4748
import javax.ws.rs.POST;
4849
import javax.ws.rs.PUT;
4950
import javax.ws.rs.Path;
@@ -135,7 +136,7 @@ public HttpResourceHandler(Iterable<? extends HttpHandler> handlers, Iterable<?
135136
* @return String representation of HttpMethod from annotations or emptyString as a default.
136137
*/
137138
private Set<HttpMethod> getHttpMethods(Method method) {
138-
Set<HttpMethod> httpMethods = new HashSet<>();
139+
Set<HttpMethod> httpMethods = new HashSet<HttpMethod>();
139140

140141
if (method.isAnnotationPresent(GET.class)) {
141142
httpMethods.add(HttpMethod.GET);
@@ -149,6 +150,9 @@ private Set<HttpMethod> getHttpMethods(Method method) {
149150
if (method.isAnnotationPresent(DELETE.class)) {
150151
httpMethods.add(HttpMethod.DELETE);
151152
}
153+
if (method.isAnnotationPresent(OPTIONS.class)) {
154+
httpMethods.add(HttpMethod.OPTIONS);
155+
}
152156

153157
return Collections.unmodifiableSet(httpMethods);
154158
}
@@ -310,7 +314,8 @@ public HttpMethodInfo getDestinationMethod(HttpRequest request, HttpResponder re
310314

311315
LOG.trace("Routable destinations for request {}: {}", requestUri, routableDestinations);
312316
Iterable<String> requestUriParts = splitAndOmitEmpty(requestUri, '/');
313-
List<PatternPathRouterWithGroups.RoutableDestination<HttpResourceModel>> matchedDestinations = new ArrayList<>();
317+
List<PatternPathRouterWithGroups.RoutableDestination<HttpResourceModel>> matchedDestinations =
318+
new ArrayList<PatternPathRouterWithGroups.RoutableDestination<HttpResourceModel>>();
314319
long maxScore = 0;
315320

316321
for (PatternPathRouterWithGroups.RoutableDestination<HttpResourceModel> destination : routableDestinations) {
@@ -390,7 +395,7 @@ public void destroy(HandlerContext context) {
390395
}
391396

392397
private static <T> List<T> copyOf(Iterable<? extends T> iterable) {
393-
List<T> list = new ArrayList<>();
398+
List<T> list = new ArrayList<T>();
394399
for (T item : iterable) {
395400
list.add(item);
396401
}

‎src/main/java/io/cdap/http/internal/HttpResourceModel.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
public final class HttpResourceModel {
4949

5050
private static final Set<Class<? extends Annotation>> SUPPORTED_PARAM_ANNOTATIONS =
51-
Collections.unmodifiableSet(new HashSet<>(Arrays.asList(PathParam.class, QueryParam.class, HeaderParam.class)));
51+
Collections.unmodifiableSet(new HashSet<Class<? extends Annotation>>
52+
(Arrays.asList(PathParam.class, QueryParam.class, HeaderParam.class)));
5253

5354
private final Set<HttpMethod> httpMethods;
5455
private final String path;
@@ -211,13 +212,15 @@ private List<Map<Class<? extends Annotation>, ParameterInfo<?>>> createParameter
211212
return Collections.emptyList();
212213
}
213214

214-
List<Map<Class<? extends Annotation>, ParameterInfo<?>>> result = new ArrayList<>();
215+
List<Map<Class<? extends Annotation>, ParameterInfo<?>>> result =
216+
new ArrayList<Map<Class<? extends Annotation>, ParameterInfo<?>>>();
215217
Type[] parameterTypes = method.getGenericParameterTypes();
216218
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
217219

218220
for (int i = 2; i < parameterAnnotations.length; i++) {
219221
Annotation[] annotations = parameterAnnotations[i];
220-
Map<Class<? extends Annotation>, ParameterInfo<?>> paramAnnotations = new IdentityHashMap<>();
222+
Map<Class<? extends Annotation>, ParameterInfo<?>> paramAnnotations =
223+
new IdentityHashMap<Class<? extends Annotation>, ParameterInfo<?>>();
221224

222225
for (Annotation annotation : annotations) {
223226
Class<? extends Annotation> annotationType = annotation.annotationType();
@@ -266,7 +269,7 @@ private static final class ParameterInfo<T> {
266269
private final Converter<T, Object> converter;
267270

268271
static <V> ParameterInfo<V> create(Annotation annotation, @Nullable Converter<V, Object> converter) {
269-
return new ParameterInfo<>(annotation, converter);
272+
return new ParameterInfo<V>(annotation, converter);
270273
}
271274

272275
private ParameterInfo(Annotation annotation, @Nullable Converter<T, Object> converter) {

‎src/main/java/io/cdap/http/internal/ImmutablePair.java

+31-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package io.cdap.http.internal;
1818

19-
import java.util.Objects;
19+
import java.util.Arrays;
2020

2121
/**
2222
* An {@link ImmutablePair} consists of two elements within. The elements once set
@@ -44,7 +44,7 @@ final class ImmutablePair<A, B> {
4444
private final B second;
4545

4646
public static <A, B> ImmutablePair<A, B> of(A first, B second) {
47-
return new ImmutablePair<>(first, second);
47+
return new ImmutablePair<A, B>(first, second);
4848
}
4949

5050
/**
@@ -91,7 +91,7 @@ public String toString() {
9191
*/
9292
@Override
9393
public int hashCode() {
94-
return Objects.hash(first, second);
94+
return Arrays.hashCode(new Object[]{first, second});
9595
}
9696

9797
/**
@@ -108,6 +108,33 @@ public boolean equals(Object o) {
108108
return false;
109109
}
110110
ImmutablePair<?, ?> other = (ImmutablePair<?, ?>) o;
111-
return Objects.equals(first, other.first) && Objects.equals(second, other.second);
111+
112+
boolean fst;
113+
if (first == null) {
114+
if (other.first == null) {
115+
fst = true;
116+
} else {
117+
return false;
118+
}
119+
} else if (other.first == null) {
120+
return false;
121+
} else {
122+
fst = first.equals(other.first);
123+
}
124+
125+
boolean snd;
126+
if (second == null) {
127+
if (other.second == null) {
128+
snd = true;
129+
} else {
130+
return false;
131+
}
132+
} else if (other.second == null) {
133+
return false;
134+
} else {
135+
snd = second.equals(other.second);
136+
}
137+
138+
return fst && snd;
112139
}
113140
}

‎src/main/java/io/cdap/http/internal/ParamConvertUtils.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public final class ParamConvertUtils {
5050
private static final Map<Class<?>, Object> PRIMITIVE_DEFAULTS;
5151

5252
static {
53-
Map<Class<?>, Object> defaults = new IdentityHashMap<>();
53+
Map<Class<?>, Object> defaults = new IdentityHashMap<Class<?>, Object>();
5454
defaults.put(Boolean.TYPE, false);
5555
defaults.put(Character.TYPE, '\0');
5656
defaults.put(Byte.TYPE, (byte) 0);
@@ -276,11 +276,11 @@ private static Converter<List<String>, Object> createCollectionConverter(Type re
276276
public Object convert(List<String> values) throws Exception {
277277
Collection<? extends Comparable> collection;
278278
if (rawType == List.class) {
279-
collection = new ArrayList<>();
279+
collection = new ArrayList<Comparable>();
280280
} else if (rawType == Set.class) {
281-
collection = new LinkedHashSet<>();
281+
collection = new LinkedHashSet<Comparable>();
282282
} else {
283-
collection = new TreeSet<>();
283+
collection = new TreeSet<Comparable>();
284284
}
285285

286286
for (String value : values) {

0 commit comments

Comments
 (0)
Please sign in to comment.