001 /**
002 * Copyright (C) 2009-2011 the original author or authors.
003 * See the notice.md file distributed with this work for additional
004 * information regarding copyright ownership.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package org.fusesource.restygwt.client;
020
021 import java.util.Map;
022
023 import com.google.gwt.core.client.JavaScriptObject;
024 import com.google.gwt.http.client.Request;
025 import com.google.gwt.http.client.RequestCallback;
026 import com.google.gwt.http.client.RequestException;
027 import com.google.gwt.http.client.Response;
028 import com.google.gwt.json.client.JSONObject;
029 import com.google.gwt.json.client.JSONValue;
030 import com.google.gwt.jsonp.client.JsonpRequestBuilder;
031 import com.google.gwt.user.client.rpc.AsyncCallback;
032 import com.google.gwt.xml.client.Document;
033
034 /**
035 * A specialized method which accesses a resource as a JSONP request.
036 *
037 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
038 */
039 public class JsonpMethod extends Method {
040
041 private final Resource resource;
042 private final JsonpRequestBuilder builder = new JsonpRequestBuilder();
043
044 public JsonpMethod(Resource resource) {
045 this.resource = resource;
046 }
047
048 public JsonpMethod callbackParam(String callbackParam) {
049 builder.setCallbackParam(callbackParam);
050 return this;
051 }
052
053 public JsonpMethod failureCallbackParam(String failureCallbackParam) {
054 builder.setFailureCallbackParam(failureCallbackParam);
055 return this;
056 }
057
058 @Override
059 public Method timeout(int timeout) {
060 builder.setTimeout(timeout);
061 return this;
062 }
063
064 @Override
065 public void send(final JsonCallback callback) {
066 builder.requestObject(resource.getUri(), new AsyncCallback<JavaScriptObject>() {
067 public void onSuccess(JavaScriptObject result) {
068 callback.onSuccess(JsonpMethod.this, new JSONObject(result));
069 }
070
071 public void onFailure(Throwable caught) {
072 callback.onFailure(JsonpMethod.this, caught);
073 }
074 });
075 }
076
077 @Override
078 public void send(final TextCallback callback) {
079 builder.requestString(resource.getUri(), new AsyncCallback<String>() {
080 public void onSuccess(String result) {
081 callback.onSuccess(JsonpMethod.this, result);
082 }
083
084 public void onFailure(Throwable caught) {
085 callback.onFailure(JsonpMethod.this, caught);
086 }
087 });
088 }
089
090 /**
091 * helper method to make RestServiceClassCreator easier to maintain
092 * @param callback
093 */
094 public void send(final AsyncCallback<JavaScriptObject> callback) {
095 builder.requestObject(resource.getUri(), callback);
096 }
097
098 @Override
099 public void send(RequestCallback callback) throws RequestException {
100 throw unsupported();
101 }
102
103 @Override
104 public Method expect(int ...status) {
105 throw unsupported();
106 }
107
108 @Override
109 public Request getRequest() {
110 throw unsupported();
111 }
112
113 @Override
114 public Response getResponse() {
115 throw unsupported();
116 }
117
118 @Override
119 public Method header(String header, String value) {
120 throw unsupported();
121 }
122
123 @Override
124 public Method headers(Map<String, String> headers) {
125 throw unsupported();
126 }
127
128 @Override
129 public Method json(JSONValue data) {
130 throw unsupported();
131 }
132
133 @Override
134 public Method password(String password) {
135 throw unsupported();
136 }
137
138 @Override
139 public void send(XmlCallback callback) {
140 throw unsupported();
141 }
142
143 @Override
144 public Method text(String data) {
145 throw unsupported();
146 }
147
148 @Override
149 public Method user(String user) {
150 throw unsupported();
151 }
152
153 @Override
154 public Method xml(Document data) {
155 throw unsupported();
156 }
157
158 private UnsupportedOperationException unsupported() {
159 return new UnsupportedOperationException("The jasonp method is restricted in what it can be configured with.");
160 }
161
162 }