Skip to content

Commit

Permalink
Coap-Http-Cross-Prox: don't fail on missing content type, if no payload
Browse files Browse the repository at this point in the history
is provided as well.

Fix issue #2252

Signed-off-by: Achim Kraus <achim.kraus@cloudcoap.net>
  • Loading branch information
boaks committed Jun 28, 2024
1 parent 3c1fa64 commit b764b6d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,27 @@ public class ContentTypedEntity {
/**
* Create instance from content type and payload.
*
* @param contentType content type
* @param payload payload.
* @param contentType content type. May be {@code null}, if no payload is
* provided.
* @param payload payload. For no payload provide {@code null} or an empty
* array.
* @throws NullPointerException if contentType is null, but payload is
* provided.
* @deprecated use {@link #create(ContentType, byte[])} instead.
*/
@Deprecated
public ContentTypedEntity(ContentType contentType, byte[] payload) {
if (contentType == null) {
throw new NullPointerException("content type must not be null!");
this.payload = payload != null && payload.length > 0 ? payload : null;
if (contentType == null && this.payload != null) {
throw new NullPointerException("content type must not be null, if payload is provided!");
}
this.contentType = contentType;
this.payload = payload != null && payload.length > 0 ? payload : null;
}

/**
* Get content type.
*
* @return content type
* @return content type, or {@code null}, if not provided
*/
public ContentType getContentType() {
return contentType;
Expand All @@ -73,10 +79,14 @@ public byte[] getContent() {
/**
* Create entity producer.
*
* @return entity producer
* @return entity producer, or {@code null}, if no content type is provided.
*/
public AsyncEntityProducer createProducer() {
return AsyncEntityProducers.create(payload, contentType);
if (contentType != null) {
return AsyncEntityProducers.create(payload, contentType);
} else {
return null;
}
}

/**
Expand All @@ -88,4 +98,24 @@ public AsyncEntityProducer createProducer() {
public static AsyncEntityProducer createProducer(ContentTypedEntity entity) {
return entity != null ? entity.createProducer() : null;
}

/**
* Create instance from content type and payload.
*
* @param contentType content type. May be {@code null}, if no payload is
* provided.
* @param payload payload. For no payload provide {@code null} or an empty
* array.
* @return entity, if content type is provided, {@code null}, otherwise.
* @throws NullPointerException if contentType is null, but payload is
* provided.
* @since 3.13
*/
public static ContentTypedEntity create(ContentType contentType, byte[] payload) {
ContentTypedEntity entity = new ContentTypedEntity(contentType, payload);
if (entity.getContentType() != null) {
return entity;
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected void data(final ByteBuffer src, final boolean endOfStream) throws IOEx

@Override
protected ContentTypedEntity generateContent() throws IOException {
return new ContentTypedEntity(contentType, buffer.toByteArray());
return ContentTypedEntity.create(contentType, buffer.toByteArray());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ public ContentTypedEntity getHttpEntity(Message coapMessage) throws TranslationE
}
}
// create the entity
httpEntity = new ContentTypedEntity(contentType, payload);
httpEntity = ContentTypedEntity.create(contentType, payload);
}

return httpEntity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,9 @@ public ProxyResponseProducer getHttpResponse(HttpRequest httpRequest, Response c
if (httpEntity != null) {
// get the content-type from the entity and set the header
ContentType contentType = httpEntity.getContentType();
httpResponse.setHeader("content-type", contentType.toString());
if (contentType != null) {
httpResponse.setHeader("content-type", contentType.toString());
}
}
}
return new ProxyResponseProducer(httpResponse, ContentTypedEntity.createProducer(httpEntity));
Expand Down

0 comments on commit b764b6d

Please sign in to comment.