Skip to content

Commit

Permalink
added support for declaring the response content-type when a route is…
Browse files Browse the repository at this point in the history
… defined, example:

    <route path="echo/{echo}" action="Echo.bsh" content-type="json"/>
  see: the 'content-type' attribute above and the related web app example in modules/restpathactions/web
  • Loading branch information
florin.patrascu@gmail.com committed Sep 21, 2011
1 parent 452414a commit 8b3fcc9
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 9 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
JPublish 4 change log:

[2011.08.07]
- the RestPathActions module allows the user to optionally define request method(s) that
can be used for route matching.
[2011-09-21]
- RPAModule; added support for declaring the response content-type when a route is defined, example:
<route path="echo/{echo}" action="Echo.bsh" content-type="json"/>
see: the 'content-type' attribute above and the related web app example in modules/restpathactions/web

[2011.06.22]
- RESTPATHACTIONS demo cleanup and module improvements, see RestPathActions module's own
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.jpublish.util.FileCopyUtils;
import org.jpublish.util.PathUtilities;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.MultivaluedMap;
import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -92,7 +91,7 @@ public void execute(JPublishContext context, Configuration configuration) throws
String content = null;

final String pagePath = rm.getPage();
String viewType = null;
String viewType = rm.getContentType();

if (pagePath != null) {
RepositoryWrapper repository = (RepositoryWrapper) context.get(module.getDefaultRepository());
Expand Down Expand Up @@ -200,7 +199,7 @@ private void setResponseContentType(JPublishContext context, String path, String

private String getMimeTypeWithCharset(String mimeType, String charSet) {

StringBuffer buffer = new StringBuffer();
StringBuilder buffer = new StringBuilder();
buffer.append(mimeType).append("; charset=").append(charSet);
return buffer.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class RestPathActionModel {
private String action;
private String page;
private String methods;
private String contentType;
private Configuration configuration;
private UriTemplateProcessor templateProcessor;

Expand Down Expand Up @@ -80,11 +81,20 @@ public void setConfiguration(Configuration configuration) {
this.configuration = configuration;
}

public String getContentType() {
return contentType;
}

public void setContentType(String contentType) {
this.contentType = contentType;
}

@Override
public String toString() {
return "path='" + path + '\'' +
", action='" + action + '\'' +
", page='" + page + '\'' +
(action != null ? ", action='" + action + '\'' : "")+
(contentType != null ? ", content-type='" + contentType + '\'' : "" )+
(page != null ? ", page='" + page + '\'': "" )+
", methods='" + methods + '\'';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public void load(Configuration configuration) throws ConfigurationException {
restModel.setAction(routeConfiguration.getAttribute("action"));
restModel.setPage(routeConfiguration.getAttribute("page"));
restModel.setMethods(routeConfiguration.getAttribute("method", "GET"));
restModel.setContentType(routeConfiguration.getAttribute("content-type"));
restModel.setConfiguration(routeConfiguration);

restModels.add(restModel);
Expand Down
3 changes: 3 additions & 0 deletions modules/restpathactions/web/WEB-INF/actions/Echo.bsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import org.jpublish.module.restpathactions.*;

context.put( RestPathActionsModule.RESPONSE, "{\"echo\": \""+context.get( "echo")+"\"}");
3 changes: 2 additions & 1 deletion modules/restpathactions/web/WEB-INF/rpa-routes.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2010, Florin T.PATRASCU
~ Copyright (c) 2011, Florin T.PATRASCU
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@

<routes>
<route path="search/{query}/page/{pageNo}" action="Search.bsh" page="search/index.html"/>
<route path="echo/{echo}" action="Echo.bsh" content-type="json"/>

<route path="demo/foo" action="rest/RestDemo.bsh">
<foo>bar</foo>
Expand Down

0 comments on commit 8b3fcc9

Please sign in to comment.