Skip to content

Commit 1fddb9f

Browse files
authored
Merge pull request #51 from zeroae/i/50-parse-mime-types
I/50 parse mime types
2 parents 7ef992c + f376245 commit 1fddb9f

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

src/main/java/co/zeroae/gate/App.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import gate.corpora.DocumentImpl;
1414
import gate.util.GateException;
1515
import gate.util.persistence.PersistenceManager;
16-
import org.apache.log4j.LogManager;
17-
import org.apache.log4j.Logger;
16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
1818

1919
import java.io.ByteArrayOutputStream;
2020
import java.io.File;
@@ -46,7 +46,7 @@ public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatew
4646
"CACHE_DIR_PREFIX", "/tmp/lru/" + GATE_APP_NAME);
4747
private static final double CACHE_DIR_USAGE = .9;
4848
private static final String DIGEST_SALT = UUID.randomUUID().toString();
49-
private static final Logger logger = LogManager.getLogger(App.class);
49+
private static final Logger logger = LoggerFactory.getLogger(App.class);
5050
private static final CorpusController application = AWSXRay.createSegment(
5151
"Gate Load", App::loadApplication);
5252
private static final AppMetadata metadata = loadMetadata();
@@ -148,7 +148,7 @@ public APIGatewayProxyResponseEvent handleExecute(APIGatewayProxyRequestEvent in
148148
AWSXRay.endSubsegment();
149149
}
150150
} catch (GateException e) {
151-
logger.error(e);
151+
logger.error(e.getMessage(), e);
152152
AWSXRay.getCurrentSubsegmentOptional().ifPresent((segment -> segment.addException(e)));
153153
response.getHeaders().put("Content-Type", "application/json");
154154
return response.withStatusCode(400).withBody(Utils.asJson(
@@ -157,7 +157,7 @@ public APIGatewayProxyResponseEvent handleExecute(APIGatewayProxyRequestEvent in
157157
}}
158158
));
159159
} catch (IOException e) {
160-
logger.error(e);
160+
logger.error(e.getMessage(), e);
161161
AWSXRay.getCurrentSubsegmentOptional().ifPresent((segment -> segment.addException(e)));
162162
response.getHeaders().put("Content-Type", "application/json");
163163
return response.withStatusCode(406).withBody(Utils.asJson(

src/main/java/co/zeroae/gate/Utils.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ static String asJson(Map<String, Object> obj) {
3535
}
3636

3737
static String ensureValidRequestContentType(String contentType) throws GateException {
38-
final String rv = contentType.equals("application/json") ? "text/json" : contentType;
39-
if (!DocumentFormat.getSupportedMimeTypes().contains(rv)) {
38+
final MimeType mimeType = DocumentFormat.getMimeTypeForString(
39+
contentType.equals("application/json") ? "text/json" : contentType);
40+
if (mimeType == null) {
4041
throw new GateException(
4142
"Unsupported MIME type " + contentType + " valid options are "
4243
+ Arrays.toString(DocumentFormat.getSupportedMimeTypes()
@@ -46,7 +47,7 @@ static String ensureValidRequestContentType(String contentType) throws GateExcep
4647
.toArray())
4748
);
4849
}
49-
return rv;
50+
return mimeType.toString();
5051
}
5152

5253
static String ensureValidResponseType(String acceptHeader) throws GateException {

src/test/java/co/zeroae/gate/AppTest.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
import java.io.IOException;
2525
import java.io.StringReader;
2626
import java.io.UnsupportedEncodingException;
27-
import java.util.*;
27+
import java.util.Collections;
28+
import java.util.HashMap;
29+
import java.util.Random;
30+
import java.util.UUID;
2831

2932
import static com.github.stefanbirkner.systemlambda.SystemLambda.withEnvironmentVariable;
3033
import static org.junit.Assert.*;
@@ -38,7 +41,7 @@ public static void setUpClass() throws Exception {
3841
}
3942

4043
private static App app = null;
41-
private static TestContext context = new TestContext();
44+
private static final TestContext context = new TestContext();
4245

4346
@Before
4447
public void setUp() {
@@ -213,7 +216,10 @@ public void testInputTypes() throws GateException {
213216
"text/x-json-datasift",
214217
"text/x-json-twitter",
215218
};
216-
for (String type: types) assertNotNull(Utils.ensureValidRequestContentType(type));
219+
for (String type : types) assertNotNull(Utils.ensureValidRequestContentType(type));
220+
221+
// This replicates issue #50
222+
assertNotNull(Utils.ensureValidRequestContentType("text/plain; charset=UTF-8"));
217223
}
218224

219225
@Test

0 commit comments

Comments
 (0)