Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

HTML5CorsFilter example

This project is a simple example to enable our WebApp to be called by Ajax in Cross-Domain context.

We use the HTML5 Cors definition to enable that.

Here an example of call:

<!DOCTYPE html>
<html lang="en">
	<head>
		<title>Web services App</title>
		<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no">
		<meta charset="utf-8">
		
		<script type="text/javascript" charset="utf-8" src="proxy.php?url=https%3A%2F%2Fgithub.com%2F.%2Fresources%2Fjquery-1.8.2.min.js"></script>
		<script type="text/javascript" charset="utf-8">
			window.jQuery && (function($){
				$(document).ready(function(){
					$("#call-rest-service").on("click", function(event){
						event.preventDefault();
						
						$.ajax(
							"http://localhost:8082/sample-backend/rest/service", // Not the same domain ! We are on http://localhost:8080 !
							{
								"async": true,
								"contentType": "application/json",
								"dataType": "json",
								"error": function(jqXHR, textStatus, errorThrown){
									alert("an error occured");
								},
								"processData": false,
								"success": function(data, textStatus, jqXHR) {
									alert(data);
								},
								"timeout": 60 * 1000,
								"type": "GET"
							}
						);
					});
				});				
			})(jQuery);
		</script>
	</head>
	
	<body>
		<h1>The REST Web services are deployed !</h1>
		
		<hr />
		<br />
		<h3>Button for test</h3>
		<button id="call-rest-service">Call rest service</button>
	</body>
</html>

Et directement, le code d'exemple:

package com.objetdirect.html5corsfilter.example.filters;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.filter.GenericFilterBean;

public class HTML5CorsFilter extends GenericFilterBean {
    private static final Logger logger = Logger.getLogger( HTML5CorsFilter.class.getName() );
 
    /**
     * {@inheritDoc}
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
 
        logger.log( Level.INFO, "HTML5CorsFilter add HTML5 CORS Headers" );
 
        HttpServletResponse res = (HttpServletResponse) response;
        res.addHeader("Access-Control-Allow-Origin", "*");
        res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
        res.addHeader("Access-Control-Allow-Headers", "Content-Type");
        chain.doFilter(request, response);
    }
}

The MIT License

Copyright (c) <2013>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.