-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
ResponseBodySerializerFactory.java
99 lines (88 loc) · 3.93 KB
/
ResponseBodySerializerFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* 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.
*/
package org.apache.shardingsphere.elasticjob.restful.serializer;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.elasticjob.restful.serializer.factory.SerializerFactory;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap;
/**
* Response body serializer factory.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ResponseBodySerializerFactory {
private static final Map<String, ResponseBodySerializer> RESPONSE_BODY_SERIALIZERS = new ConcurrentHashMap<>();
private static final Map<String, SerializerFactory> RESPONSE_BODY_SERIALIZER_FACTORIES = new ConcurrentHashMap<>();
private static final ResponseBodySerializer MISSING_SERIALIZER = new ResponseBodySerializer() {
@Override
public String mimeType() {
throw new UnsupportedOperationException();
}
@Override
public byte[] serialize(final Object responseBody) {
throw new UnsupportedOperationException();
}
};
static {
for (ResponseBodySerializer serializer : ServiceLoader.load(ResponseBodySerializer.class)) {
RESPONSE_BODY_SERIALIZERS.put(serializer.mimeType(), serializer);
}
for (SerializerFactory factory : ServiceLoader.load(SerializerFactory.class)) {
RESPONSE_BODY_SERIALIZER_FACTORIES.put(factory.mimeType(), factory);
}
}
/**
* Get serializer for specific HTTP content type.
*
* <p>
* This method will look for a serializer instance of specific MIME type.
* If serializer not found, this method would look for serializer factory by MIME type.
* If it is still not found, the MIME type would be marked as <code>MISSING_SERIALIZER</code>.
* </p>
*
* <p>
* Some default serializer will be provided by {@link SerializerFactory},
* so developers can implement {@link ResponseBodySerializer} and register it by SPI to override default serializer.
* </p>
*
* @param contentType HTTP content type
* @return Serializer
*/
public static ResponseBodySerializer getResponseBodySerializer(final String contentType) {
ResponseBodySerializer serializer = RESPONSE_BODY_SERIALIZERS.get(contentType);
if (null == serializer) {
synchronized (ResponseBodySerializerFactory.class) {
if (null == RESPONSE_BODY_SERIALIZERS.get(contentType)) {
serializer = getResponseBodySerializerFromFactories(contentType);
}
}
}
return serializer != MISSING_SERIALIZER ? serializer : null;
}
private static ResponseBodySerializer getResponseBodySerializerFromFactories(final String contentType) {
ResponseBodySerializer serializer;
SerializerFactory factory = RESPONSE_BODY_SERIALIZER_FACTORIES.get(contentType);
if (null != factory) {
serializer = factory.createSerializer();
} else {
serializer = MISSING_SERIALIZER;
}
RESPONSE_BODY_SERIALIZERS.put(contentType, serializer);
return serializer;
}
}