| 1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 |
<!-- |
| 3 |
|
| 4 |
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 5 |
|
| 6 |
Copyright (c) 2010-2011 Oracle and/or its affiliates. All rights reserved. |
| 7 |
|
| 8 |
The contents of this file are subject to the terms of either the GNU |
| 9 |
General Public License Version 2 only ("GPL") or the Common Development |
| 10 |
and Distribution License("CDDL") (collectively, the "License"). You |
| 11 |
may not use this file except in compliance with the License. You can |
| 12 |
obtain a copy of the License at |
| 13 |
http://glassfish.java.net/public/CDDL+GPL_1_1.html |
| 14 |
or packager/legal/LICENSE.txt. See the License for the specific |
| 15 |
language governing permissions and limitations under the License. |
| 16 |
|
| 17 |
When distributing the software, include this License Header Notice in each |
| 18 |
file and include the License file at packager/legal/LICENSE.txt. |
| 19 |
|
| 20 |
GPL Classpath Exception: |
| 21 |
Oracle designates this particular file as subject to the "Classpath" |
| 22 |
exception as provided by Oracle in the GPL Version 2 section of the License |
| 23 |
file that accompanied this code. |
| 24 |
|
| 25 |
Modifications: |
| 26 |
If applicable, add the following below the License Header, with the fields |
| 27 |
enclosed by brackets [] replaced by your own identifying information: |
| 28 |
"Portions Copyright [year] [name of copyright owner]" |
| 29 |
|
| 30 |
Contributor(s): |
| 31 |
If you wish your version of this file to be governed by only the CDDL or |
| 32 |
only the GPL Version 2, indicate your decision by adding "[Contributor] |
| 33 |
elects to include this software in this distribution under the [CDDL or GPL |
| 34 |
Version 2] license." If you don't indicate a single choice of license, a |
| 35 |
recipient has the option to distribute your version of this file under |
| 36 |
either the CDDL, the GPL Version 2 or to extend the choice of license to |
| 37 |
its licensees as provided above. However, if you add GPL Version 2 code |
| 38 |
and therefore, elected the GPL Version 2 license, then the option applies |
| 39 |
only if the new code is made subject to such option by the copyright |
| 40 |
holder. |
| 41 |
|
| 42 |
--> |
| 43 |
<html> |
| 44 |
<head id="scriptComesHere"> |
| 45 |
<title>Jsonp Example</title> |
| 46 |
<style type="text/css"><!-- |
| 47 |
.figure { text-align:center; |
| 48 |
margin: 20px} |
| 49 |
.cliSample { background-color:lightgray } |
| 50 |
.odd { background:lightgray} |
| 51 |
.even { background:white} |
| 52 |
.footer {font-size: 8pt; font-style:italic;padding-top:4pt;} |
| 53 |
--></style> |
| 54 |
<script language="javascript">//<![CDATA[ |
| 55 |
function callback(data) { |
| 56 |
var len = data.length; |
| 57 |
var resultDiv = document.getElementById("writehere"); |
| 58 |
resultDiv.innerHTML = ""; |
| 59 |
var rowStyle = ["odd", "even"]; |
| 60 |
for (i=0; i<len; i++) { |
| 61 |
var rowObject = document.createElement("div"); |
| 62 |
rowObject.setAttribute("class", rowStyle[i%2]); |
| 63 |
rowObject.innerHTML = data[i].logMessage + ", " + data[i].linesChanged + " line(s) changed" + (data[i].madeByAdmin ? " by admin" : ""); |
| 64 |
resultDiv.appendChild(rowObject); |
| 65 |
} |
| 66 |
var footerObject = document.createElement("div"); |
| 67 |
footerObject.setAttribute("class", "footer"); |
| 68 |
footerObject.innerHTML = len + " change(s) found"; |
| 69 |
resultDiv.appendChild(footerObject); |
| 70 |
}//]]</script> |
| 71 |
</head> |
| 72 |
<body> |
| 73 |
<h1>JSONP Example</h1> |
| 74 |
<p>This example demonstrates how to use JSONP wrapper in order to serve |
| 75 |
not only JSON but also a JavaScript expression representing a function call with the original JSON data as a parameter. |
| 76 |
The technique is well described e.g. at <a href="http://en.wikipedia.org/wiki/JSON#JSONP">http://en.wikipedia.org/wiki/JSON#JSONP</a>. |
| 77 |
<p>A simple web application is presented with just one resource: a change record list. A client can obtain |
| 78 |
the list using XML or JSON or JavaScript representation. The last change record is also available in all three formats |
| 79 |
as a subresource of the previously mentioned list resource. |
| 80 |
|
| 81 |
|
| 82 |
<h2>Contents</h2> |
| 83 |
<p>The change record list web resource is implemented by <code>com.sun.jersey.samples.jsonp.resources.ChangeList</code> class. |
| 84 |
<p>The mapping of the URI path space is presented in the following table:</p> |
| 85 |
<table border="1"> |
| 86 |
<tr> |
| 87 |
<th>URI path</th> |
| 88 |
<th>Resource class</th> |
| 89 |
<th>HTTP methods</th> |
| 90 |
</tr> |
| 91 |
|
| 92 |
<tr> |
| 93 |
<td>/changes</td> |
| 94 |
<td>ChangeList</td> |
| 95 |
<td>GET</td> |
| 96 |
</tr> |
| 97 |
|
| 98 |
<tr> |
| 99 |
<td>/changes/latest</td> |
| 100 |
<td>ChangeList</td> |
| 101 |
<td>GET</td> |
| 102 |
</tr> |
| 103 |
|
| 104 |
</table> |
| 105 |
|
| 106 |
<h2>Running the Example</h2> |
| 107 |
<p>Run the example as follows:</p> |
| 108 |
<blockquote><pre> |
| 109 |
mvn clean compile exec:java</pre></blockquote> |
| 110 |
<p>This deploys the JSONP example using |
| 111 |
<a href="http://grizzly.java.net/">Grizzly</a> |
| 112 |
</p> |
| 113 |
<p>A <a href="http://wadl.java.net/#spec">WADL description</a> may be then accessed at the URL:</p> |
| 114 |
<blockquote><code><a href="http://localhost:9998/jsonp/application.wadl">http://localhost:9998/jsonp/application.wadl</a></code></blockquote> |
| 115 |
<p>Following steps are using <a href="http://curl.haxx.se/">cURL</a> command line tool: |
| 116 |
<p>Get the list of changes in JSON format:</p> |
| 117 |
<blockquote><pre>curl -HAccept:application/json http://localhost:9998/jsonp/changes</pre></blockquote> |
| 118 |
|
| 119 |
<p>Get the list of changes in XML format:</p> |
| 120 |
<blockquote><pre>curl -HAccept:application/xml http://localhost:9998/jsonp/changes</pre></blockquote> |
| 121 |
|
| 122 |
<p>Get the list of changes as a JavaScript callback:</p> |
| 123 |
<blockquote><pre>curl -HAccept:application/x-javascript http://localhost:9998/jsonp/changes</pre></blockquote> |
| 124 |
|
| 125 |
<p>You can also see test classes at <code>src/main/test</code> subdirectory |
| 126 |
for detail information how to consume the service using Jersey client API</p> |
| 127 |
|
| 128 |
<h3>JavaScript Client</h3> |
| 129 |
<p>After starting the service and refreshing this page, a list of changes should be displayed below: |
| 130 |
<div id="writehere"> |
| 131 |
<script language="javascript">//<![CDATA[ |
| 132 |
var headLoc = document.getElementById("scriptComesHere"); |
| 133 |
var scriptObj = document.createElement("script"); |
| 134 |
scriptObj.setAttribute("type", "application/x-javascript"); |
| 135 |
scriptObj.setAttribute("src", "http://localhost:9998/jsonp/changes"); |
| 136 |
headLoc.appendChild(scriptObj); |
| 137 |
//]]</script> |
| 138 |
You need to run the service first! |
| 139 |
</div> |
| 140 |
|
| 141 |
</body> |
| 142 |
</html> |