-
Notifications
You must be signed in to change notification settings - Fork 838
Simplify ResponseParser.getContentType processing #4528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| title: > | ||
| Simplify SolrJ ResponseParser.getContentType processing. getContentType must now return a Set. | ||
| type: other | ||
| authors: | ||
| - name: David Smiley | ||
| links: | ||
| - name: PR#4528 | ||
| url: https://github.com/apache/solr/pull/4528 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,8 @@ | |
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.util.Collection; | ||
| import java.util.Locale; | ||
| import java.util.Set; | ||
| import org.apache.solr.common.util.NamedList; | ||
|
|
||
| /** | ||
|
|
@@ -28,17 +30,40 @@ | |
| */ | ||
| public abstract class ResponseParser { | ||
|
|
||
| protected ResponseParser() { | ||
| assert validateContentTypes(); | ||
| } | ||
|
|
||
| private boolean validateContentTypes() { | ||
| Collection<String> contentTypes = getContentTypes(); | ||
| assert contentTypes == getContentTypes() | ||
| : getClass().getName() + ".getContentTypes() must return the same instance on every call"; | ||
| for (String ct : contentTypes) { | ||
| assert !ct.contains(";") && ct.equals(ct.toLowerCase(Locale.ROOT)) | ||
| : getClass().getName() | ||
| + ".getContentTypes() must return lowercase MIME types without semicolons, got: " | ||
| + ct; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| /** The writer type placed onto the request as the {@code wt} param. */ | ||
| public abstract String getWriterType(); // for example: wt=XML, JSON, etc | ||
|
|
||
| public abstract NamedList<Object> processResponse(InputStream body, String encoding) | ||
| throws IOException; | ||
|
|
||
| /** | ||
| * A well-behaved ResponseParser will return the content-types it supports. | ||
| * Returns the MIME types this parser supports. Return an empty set to disable. Implementations | ||
| * must: | ||
| * | ||
| * <ul> | ||
| * <li>Returns the same instance (same reference on every call). | ||
| * <li>Use only lowercase MIME types without charset or other parameters (no semicolons) | ||
| * <li> | ||
| * </ul> | ||
| * | ||
| * @return the content-type values that this parser is capable of parsing. Never null. Empty means | ||
| * no enforcement. | ||
| * @return the MIME types that this parser is capable of parsing. Never null. | ||
| */ | ||
| public abstract Collection<String> getContentTypes(); | ||
| public abstract Set<String> getContentTypes(); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a backwards incompatible change... and it it's only caller should be our own code, and I think very few people would have a custom one of these. |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -437,29 +437,6 @@ public void testUseOptionalCredentialsWithNull() throws Exception { | |
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testProcessorMimeTypes() throws Exception { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. from Claude: The deleted test was really checking the behavior of processorAcceptsMimeType — specifically, that it correctly stripped the ; charset=UTF-8 from "application/xml; Now that getContentTypes() returns pre-normalized MIME types and checkContentType just does contains(mimeType.toLowerCase(...)), the interesting logic has moved into the assertTrue(new XMLResponseParser().getContentTypes().contains("application/xml")); That's testing the parser, not the client. It would belong in a XMLResponseParserTest or similar, not in an HTTP client integration test. And since it's just verifying I'd leave it deleted. |
||
| ResponseParser rp = new XMLResponseParser(); | ||
|
|
||
| try (HttpJdkSolrClient client = | ||
| builder(solrTestRule.getBaseUrl()).withResponseParser(rp).build()) { | ||
| assertTrue(client.processorAcceptsMimeType(rp.getContentTypes(), "application/xml")); | ||
| assertFalse(client.processorAcceptsMimeType(rp.getContentTypes(), "application/json")); | ||
| queryToHelpJdkReleaseThreads(client); | ||
| } | ||
|
|
||
| rp = new JavaBinResponseParser(); | ||
| try (HttpJdkSolrClient client = | ||
| builder(solrTestRule.getBaseUrl()).withResponseParser(rp).build()) { | ||
| assertTrue( | ||
| client.processorAcceptsMimeType( | ||
| rp.getContentTypes(), "application/vnd.apache.solr.javabin")); | ||
| assertTrue(client.processorAcceptsMimeType(rp.getContentTypes(), "application/octet-stream")); | ||
| assertFalse(client.processorAcceptsMimeType(rp.getContentTypes(), "application/xml")); | ||
| queryToHelpJdkReleaseThreads(client); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testContentTypeToEncoding() throws Exception { | ||
| try (HttpJdkSolrClient client = builder(solrTestRule.getBaseUrl()).build()) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The best place for validation is here when instantiating. These are new rules... if someone actually has a custom non-compliant impl, they'll find out.