Skip to content

Commit

Permalink
Merge pull request #319 from sumeetphadnis/next
Browse files Browse the repository at this point in the history
Support for adding qrest response headers
  • Loading branch information
ar authored Dec 5, 2024
2 parents a83854f + cdbbe5e commit 7ad0033
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
4 changes: 4 additions & 0 deletions modules/qrest/src/dist/deploy/30_qrest_txnmgr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<route path="/q2**" method="POST" name="q2"/>
<route path="/metrics" method="GET" name="metrics"/>
<route path="/test/load_file" method="POST" name="upload_file"/>
<route path="/test/resp_header" method="POST" name="resp_header"/>
<route path="/welcome.html" method="GET" name="welcome" />
<route path="/dynamic" method="GET" name="dynamic" />
<route path="/" method="GET" name="index" />
Expand All @@ -31,6 +32,9 @@
<participant class="org.jpos.qrest.ExtractFile" />
<participant class="org.jpos.qrest.test.participant.DumpFile" enabled="${test.enabled:false}" />
</group>
<group name="resp_header">
<participant class="org.jpos.qrest.test.participant.SetRespHeaders" enabled="${test.enabled:false}" />
</group>
<group name="index">
<participant class="org.jpos.qrest.participant.StaticContent">
<property name="documentRoot" value="html" />
Expand Down
9 changes: 9 additions & 0 deletions modules/qrest/src/main/java/org/jpos/qrest/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@

import io.netty.handler.codec.http.HttpResponseStatus;

import java.util.HashMap;
import java.util.Map;

public class Response {
private HttpResponseStatus status;
private Object body;
private String contentType;
private Map<String, String> headers;

public Response(HttpResponseStatus status, Object body) {
this(status, body, null);
Expand All @@ -33,6 +37,7 @@ public Response(HttpResponseStatus status, Object body, String contentType) {
this.status = status;
this.body = body;
this.contentType = contentType;
this.headers = new HashMap<>();
}

public HttpResponseStatus status() {
Expand All @@ -47,6 +52,10 @@ public String contentType() {
return contentType;
}

public Map<String, String> getHeaders() {
return headers;
}

@Override
public String toString() {
return "Response{" +
Expand Down
5 changes: 5 additions & 0 deletions modules/qrest/src/main/java/org/jpos/qrest/SendResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.jpos.transaction.Context;

import java.io.Serializable;
import java.util.Map;

import static io.netty.buffer.Unpooled.copiedBuffer;
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE;
Expand Down Expand Up @@ -122,6 +123,10 @@ else if (response.body() instanceof String)

HttpHeaders httpHeaders = httpResponse.headers();

for (Map.Entry<String, String> header : response.getHeaders().entrySet()) {
httpHeaders.add(header.getKey(), header.getValue());
}

if (response.contentType() != null)
httpResponse.headers().set(CONTENT_TYPE, response.contentType());
else if (isJson)
Expand Down
12 changes: 12 additions & 0 deletions modules/qrest/src/test/java/org/jpos/qrest/RestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,16 @@ void testUploadFile() throws Exception {
.assertThat()
.body("content", equalTo("hello"));
}

@Test
void testResponseHeader() throws Exception {
given().log().all()
.post("/test/resp_header")
.then()
.statusCode(200)
.assertThat()
.header("test-content-header", equalTo("hello"))
.and()
.body("content", equalTo("world"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.jpos.qrest.test.participant;

import io.netty.handler.codec.http.HttpResponseStatus;
import org.jpos.qrest.Response;
import org.jpos.transaction.Context;
import org.jpos.transaction.TransactionParticipant;

import java.io.Serializable;
import java.util.Collections;

import static org.jpos.qrest.Constants.RESPONSE;

public class SetRespHeaders implements TransactionParticipant {


@Override
public int prepare(long id, Serializable context) {

Context ctx = (Context) context;

try {
Response response = new Response(HttpResponseStatus.OK, Collections.singletonMap("content", "world"));
response.getHeaders().put("test-content-header","hello");
ctx.put(RESPONSE, response);
return PREPARED | NO_JOIN | READONLY;
} catch (Exception e) {
ctx.log(e);
ctx.put(RESPONSE, new Response(HttpResponseStatus.INTERNAL_SERVER_ERROR, Collections.emptyMap()));
return ABORTED;
}
}

}

0 comments on commit 7ad0033

Please sign in to comment.