From bd907cea70c2fcddd444cd2b9d8e2a29255ddcd7 Mon Sep 17 00:00:00 2001 From: Alex Osborne Date: Wed, 28 Jun 2023 14:17:53 +0900 Subject: [PATCH] Add MediaType.raw() --- src/org/netpreserve/jwarc/MediaType.java | 31 +++++++++++++++--------- src/org/netpreserve/jwarc/MediaType.rl | 18 +++++++++++--- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/org/netpreserve/jwarc/MediaType.java b/src/org/netpreserve/jwarc/MediaType.java index 67cff5e..c0bdc12 100644 --- a/src/org/netpreserve/jwarc/MediaType.java +++ b/src/org/netpreserve/jwarc/MediaType.java @@ -2,7 +2,7 @@ // line 1 "MediaType.rl" /* * SPDX-License-Identifier: Apache-2.0 - * Copyright (C) 2018 National Library of Australia and the jwarc contributors + * Copyright (C) 2018-2023 National Library of Australia and the jwarc contributors */ // recompile: ragel -J MediaType.rl -o MediaType.java @@ -168,10 +168,11 @@ private static byte[] init__media_type_eof_actions_0() public static MediaType HTTP_REQUEST = MediaType.parse("application/http;msgtype=request"); public static MediaType HTTP_RESPONSE = MediaType.parse("application/http;msgtype=response"); public static MediaType OCTET_STREAM = MediaType.parse("application/octet-stream"); - public static MediaType PLAIN_TEXT = MediaType.parse("text/plain"); + public static MediaType PLAIN_TEXT = MediaType.parse("text/plain"); public static MediaType WARC_FIELDS = MediaType.parse("application/warc-fields"); public static final MediaType WWW_FORM_URLENCODED = MediaType.parse("application/x-www-form-urlencoded"); + private final String raw; private final String type; private final String subtype; private final Map parameters; @@ -194,14 +195,14 @@ public static MediaType parse(String string) { StringBuilder buf = new StringBuilder(); -// line 197 "MediaType.java" +// line 199 "MediaType.java" { cs = media_type_start; } -// line 95 "MediaType.rl" +// line 97 "MediaType.rl" -// line 204 "MediaType.java" +// line 206 "MediaType.java" { int _klen; int _trans = 0; @@ -341,7 +342,7 @@ else if ( ( string.charAt(p)) > _media_type_trans_keys[_mid+1] ) // line 48 "MediaType.rl" { subtypeEnd = p; } break; -// line 344 "MediaType.java" +// line 346 "MediaType.java" } } } @@ -390,7 +391,7 @@ else if ( ( string.charAt(p)) > _media_type_trans_keys[_mid+1] ) // line 48 "MediaType.rl" { subtypeEnd = p; } break; -// line 393 "MediaType.java" +// line 395 "MediaType.java" } } } @@ -400,20 +401,28 @@ else if ( ( string.charAt(p)) > _media_type_trans_keys[_mid+1] ) break; } } -// line 96 "MediaType.rl" +// line 98 "MediaType.rl" String type = string.substring(0, typeEnd); String subtype = string.substring(typeEnd + 1, subtypeEnd); Map parameters = Collections.unmodifiableMap(map); - return new MediaType(type, subtype, parameters); + return new MediaType(string, type, subtype, parameters); } - private MediaType(String type, String subtype, Map parameters) { + private MediaType(String raw, String type, String subtype, Map parameters) { + this.raw = raw; this.type = type; this.subtype = subtype; this.parameters = parameters; } + /** + * The original unparsed media type string. + */ + public String raw() { + return raw; + } + public String type() { return type; } @@ -478,7 +487,7 @@ public String toString() { * The base type and subtype without any parameters. */ public MediaType base() { - return new MediaType(type, subtype, Collections.emptyMap()); + return new MediaType(null, type, subtype, Collections.emptyMap()); } private static boolean validToken(String s) { diff --git a/src/org/netpreserve/jwarc/MediaType.rl b/src/org/netpreserve/jwarc/MediaType.rl index 375dc73..10fc937 100644 --- a/src/org/netpreserve/jwarc/MediaType.rl +++ b/src/org/netpreserve/jwarc/MediaType.rl @@ -1,6 +1,6 @@ /* * SPDX-License-Identifier: Apache-2.0 - * Copyright (C) 2018 National Library of Australia and the jwarc contributors + * Copyright (C) 2018-2023 National Library of Australia and the jwarc contributors */ // recompile: ragel -J MediaType.rl -o MediaType.java @@ -67,9 +67,11 @@ public class MediaType extends MessageParser { public static MediaType HTTP_REQUEST = MediaType.parse("application/http;msgtype=request"); public static MediaType HTTP_RESPONSE = MediaType.parse("application/http;msgtype=response"); public static MediaType OCTET_STREAM = MediaType.parse("application/octet-stream"); + public static MediaType PLAIN_TEXT = MediaType.parse("text/plain"); public static MediaType WARC_FIELDS = MediaType.parse("application/warc-fields"); public static final MediaType WWW_FORM_URLENCODED = MediaType.parse("application/x-www-form-urlencoded"); + private final String raw; private final String type; private final String subtype; private final Map parameters; @@ -97,15 +99,23 @@ public class MediaType extends MessageParser { String type = string.substring(0, typeEnd); String subtype = string.substring(typeEnd + 1, subtypeEnd); Map parameters = Collections.unmodifiableMap(map); - return new MediaType(type, subtype, parameters); + return new MediaType(string, type, subtype, parameters); } - private MediaType(String type, String subtype, Map parameters) { + private MediaType(String raw, String type, String subtype, Map parameters) { + this.raw = raw; this.type = type; this.subtype = subtype; this.parameters = parameters; } + /** + * The original unparsed media type string. + */ + public String raw() { + return raw; + } + public String type() { return type; } @@ -170,7 +180,7 @@ public class MediaType extends MessageParser { * The base type and subtype without any parameters. */ public MediaType base() { - return new MediaType(type, subtype, Collections.emptyMap()); + return new MediaType(null, type, subtype, Collections.emptyMap()); } private static boolean validToken(String s) {