-
Notifications
You must be signed in to change notification settings - Fork 217
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
728 payload content-type check & error handling #738
Changes from 1 commit
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 |
---|---|---|
@@ -1,25 +1,39 @@ | ||
package pl.allegro.tech.hermes.frontend.publishing.message; | ||
|
||
import org.apache.avro.Schema; | ||
import pl.allegro.tech.hermes.common.message.wrapper.UnsupportedContentTypeException; | ||
import tech.allegro.schema.json2avro.converter.JsonAvroConverter; | ||
|
||
import static javax.ws.rs.core.MediaType.APPLICATION_JSON; | ||
import static pl.allegro.tech.hermes.api.AvroMediaType.AVRO_BINARY; | ||
|
||
public class MessageContentTypeEnforcer { | ||
|
||
private final JsonAvroConverter converter = new JsonAvroConverter(); | ||
|
||
private static final String APPLICATION_JSON_WITH_DELIM = APPLICATION_JSON + ";"; | ||
private static final String AVRO_BINARY_WITH_DELIM = AVRO_BINARY + ";"; | ||
|
||
public byte[] enforceAvro(String payloadContentType, byte[] data, Schema schema) { | ||
if (isJSON(payloadContentType)) { | ||
return converter.convertToAvro(data, schema); | ||
} else if (isAvro(payloadContentType)) { | ||
return data; | ||
} else { | ||
throw new UnsupportedContentTypeException(payloadContentType, schema); | ||
} | ||
return data; | ||
} | ||
|
||
private boolean isJSON(String contentType) { | ||
return contentType != null && (contentType.length() > APPLICATION_JSON.length() ? | ||
contentType.startsWith(APPLICATION_JSON_WITH_DELIM) : contentType.equals(APPLICATION_JSON)); | ||
return isOfType(contentType, APPLICATION_JSON, APPLICATION_JSON_WITH_DELIM); | ||
} | ||
|
||
private boolean isAvro(String contentType) { | ||
return isOfType(contentType, AVRO_BINARY, AVRO_BINARY_WITH_DELIM); | ||
} | ||
|
||
private boolean isOfType(String contentType, String expectedContentType, String expectedWithDelim) { | ||
return contentType != null && (contentType.length() > expectedContentType.length() ? | ||
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. shouldn't we just check 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. also, content type is not case sensitive (https://www.w3.org/Protocols/rfc1341/4_Content-Type.html) so we can match with 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. checking with startsWith doesn't enforce ';' to be the first character after content-type value. 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. no, the current algorithm is fine 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. Good point.
|
||
contentType.startsWith(expectedWithDelim) : contentType.equals(expectedContentType)); | ||
} | ||
} |
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.
why Avro Schema here? i don't get it, we probably want topic name, not schema name
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.
That's right. In most cases schema name matches topic name, and tests didn't catch that typo.
Fixed