Skip to content

Commit

Permalink
manually set json response
Browse files Browse the repository at this point in the history
  • Loading branch information
grobmeier committed Apr 3, 2012
1 parent 9d147a1 commit 07f6f2f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class JsonResult extends StrutsResultSupport {

private boolean commentOutput = true;

private String jsonResponse;

/** Default constructor */
public JsonResult() {
}
Expand Down Expand Up @@ -84,7 +86,19 @@ protected void doExecute(String finalLocation, ActionInvocation invocation)

PrintWriter writer = response.getWriter();
try {
if (!ActionSupport.SUCCESS.equals(invocation.getResultCode())) {
if(this.jsonResponse != null) {
if (this.commentOutput) {
log.debug("JSON will be served with comments - change with param: outputComment = false ");
writer.write("/* ");
writer.write(this.jsonResponse);
writer.write(" */");
} else {
writer.write(this.jsonResponse);
}
return;
}

if (!ActionSupport.SUCCESS.equals(invocation.getResultCode())) {
if(ActionSupport.LOGIN.equals(invocation.getResultCode())) {
response.sendError(401, "Not authorized");
return;
Expand All @@ -99,6 +113,7 @@ protected void doExecute(String finalLocation, ActionInvocation invocation)
writer.write(new char[] { 'n', 'u', 'l', 'l' }, 0, 4);
return;
}

Object obj = invocation.getAction();
JSONAnnotationEncoder encoder = new JSONAnnotationEncoder();
String result = encoder.encode(obj);
Expand All @@ -119,6 +134,14 @@ protected void doExecute(String finalLocation, ActionInvocation invocation)
}
}

/**
* Sets the response for this result. Once the response is set, the actions
* will not be serialized anymore.
*/
public void setJsonResponse(String response) {
this.jsonResponse = response;
}

/**
* Set the character set
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,10 @@ public void testMultiline() throws Exception {
System.out.println(executeAction);
Assert.assertEquals(expected, executeAction);
}

@Test
public void testSetJsonResponse() throws Exception {
String executeAction = this.executeAction("/test3");
Assert.assertEquals("/* {\"hello\":\"world\"} */", executeAction);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2011 Christian Grobmeier
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package de.grobmeier.json.plugins.struts2;

import com.opensymphony.xwork2.ActionSupport;
import de.grobmeier.jjson.convert.JSON;

public class SendToLoginAction extends ActionSupport {
public String execute(){
return LOGIN;
}
}
16 changes: 16 additions & 0 deletions jjson-struts2/src/test/resources/struts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,20 @@
<result type="json" />
</action>
</package>

<package name="globalResponse" extends="struts-default">
<result-types>
<result-type name="json" class="de.grobmeier.json.plugins.struts2.JsonResult" />
</result-types>

<global-results>
<result name="login" type="json">
<param name="jsonResponse">{"hello":"world"}</param>
</result>
</global-results>

<action name="test3" class="de.grobmeier.json.plugins.struts2.SendToLoginAction">
<result type="json" />
</action>
</package>
</struts>

0 comments on commit 07f6f2f

Please sign in to comment.