add CORs filter for backend platform and hook view all offices up to it.

This commit is contained in:
Keith Woodlock
2012-05-11 00:55:42 +01:00
parent 6f4a47a1ca
commit 85ac895bb9
3 changed files with 67 additions and 14 deletions
@@ -53,9 +53,9 @@
return false;
}
},
globalDate: function(dateParts) {
globalDate: function(localDateAsISOString) {
try {
var dateParts = localDateAsISOString.split("-")
var year = dateParts[0];
var month = parseInt(dateParts[1]) - 1; // month is zero indexed
var day = dateParts[2];
@@ -68,9 +68,9 @@
return "??";
}
},
globalDateTime: function(dateParts) {
globalDateTime: function(dateInMillis) {
try {
var d = new Date(dateParts);
var d = new Date(dateInMillis);
return Globalize.format(d,"F");
} catch(e) {
@@ -165,10 +165,10 @@
}).dialog('open');
}
function popupDialogWithFormView(url, titleCode, templateSelector, width, height, saveSuccessFunction) {
function popupDialogWithFormView(getUrl, postUrl, titleCode, templateSelector, width, height, saveSuccessFunction) {
var dialogDiv = $("<div id='dialog-form'></div>");
var jqxhr = $.ajax({
url: url,
url: getUrl,
type: 'GET',
contentType: 'application/json',
dataType: 'json',
@@ -196,7 +196,7 @@
var form_data = $('#entityform').serialize();
var jqxhr = $.ajax({
url: url,
url: postUrl,
type: 'POST',
data: form_data,
success: saveSuccessFunction,
@@ -248,7 +248,10 @@
});
$('#addloanproduct').click(function(e) {
var url = '${rootContext}portfolio/product/loan/new';
var getUrl = 'http://localhost:8085/mifosng-provider/api/v1/loanproducts/template';
var postUrl = 'http://localhost:8085/mifosng-provider/api/v1/loanproducts';
//var url = '${rootContext}portfolio/product/loan/new';
var templateSelector = "#productFormTemplate";
var width = 800;
var height = 550;
@@ -258,7 +261,7 @@
refreshLoanProductsView();
}
popupDialogWithFormView(url, "dialog.title.add.loan.product", templateSelector, width, height, saveSuccessFunction);
popupDialogWithFormView(getUrl, postUrl,"dialog.title.add.loan.product", templateSelector, width, height, saveSuccessFunction);
e.preventDefault();
});
@@ -338,7 +341,8 @@
});
$('#addoffice').click(function(e) {
var url = '${rootContext}org/office/new';
var getUrl = 'http://localhost:8085/mifosng-provider/api/v1/offices/template';
var postUrl = 'http://localhost:8085/mifosng-provider/api/v1/offices';
var templateSelector = "#officeFormTemplate";
var width = 600;
var height = 400;
@@ -354,14 +358,18 @@
function refreshOfficesView() {
var jqxhr = $.ajax({
url: '${allOfficesUrl}',
url: 'http://localhost:8085/mifosng-provider/api/v1/offices', // '${allOfficesUrl}',
type: 'GET',
contentType: 'application/json',
dataType: 'json',
success: function(data, textStatus, jqXHR) {
$.each(data.offices, function(i, item) {
//alert(item.name);
});
console.log(data);
var officelistParent = new Object();
officelistParent.items = data;
officelistParent.items = data.offices;
var officeListHtml = $("#officeListTemplate").render(officelistParent);
$("#listplaceholder").html(officeListHtml);
@@ -0,0 +1,32 @@
package org.mifosng.platform.filters;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;
/**
* General approach to turn back-end capabilities exposed through resource-oriented API as Cross-Origin-Request complaint.
*/
public class ResponseCorsFilter implements ContainerResponseFilter {
@Override
public ContainerResponse filter(ContainerRequest request, ContainerResponse response) {
ResponseBuilder resp = Response.fromResponse(response.getResponse());
resp.header("Access-Control-Allow-Origin", "*").header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
String reqHead = request.getHeaderValue("Access-Control-Request-Headers");
if (null != reqHead && !reqHead.equals(null)) {
resp.header("Access-Control-Allow-Headers", reqHead);
}
response.setResponse(resp.build());
return response;
}
}
@@ -51,12 +51,25 @@
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<!-- debugging on for development -->
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<!-- debugging on for development -->
<!--
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>com.sun.jersey.api.container.filter.LoggingFilter, org.mifosng.platform.filters.ResponseCorsFilter</param-value>
</init-param>
-->
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>org.mifosng.platform.filters.ResponseCorsFilter</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>