-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
payload content-type check & error handling
message payload content-type check & error message for wrong content-type (#728)
- Loading branch information
1 parent
5142f4b
commit e304e84
Showing
9 changed files
with
78 additions
and
14 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...s/consumer/sender/http/AvroMediaType.java → ...llegro/tech/hermes/api/AvroMediaType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 21 additions & 5 deletions
26
...n/java/pl/allegro/tech/hermes/frontend/publishing/message/MessageContentTypeEnforcer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,41 @@ | ||
package pl.allegro.tech.hermes.frontend.publishing.message; | ||
|
||
import org.apache.avro.Schema; | ||
import org.apache.commons.lang.StringUtils; | ||
import pl.allegro.tech.hermes.api.Topic; | ||
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)) { | ||
public byte[] enforceAvro(String payloadContentType, byte[] data, Schema schema, Topic topic) { | ||
String contentTypeLowerCase = StringUtils.lowerCase(payloadContentType); | ||
if (isJSON(contentTypeLowerCase)) { | ||
return converter.convertToAvro(data, schema); | ||
} else if (isAvro(contentTypeLowerCase)) { | ||
return data; | ||
} else { | ||
throw new UnsupportedContentTypeException(payloadContentType, topic); | ||
} | ||
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.equals(expectedContentType) || contentType.startsWith(expectedWithDelim)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters