001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.client.rpc.impl;
017:
018: import com.google.gwt.http.client.Request;
019: import com.google.gwt.http.client.RequestCallback;
020: import com.google.gwt.http.client.Response;
021: import com.google.gwt.user.client.rpc.AsyncCallback;
022: import com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException;
023: import com.google.gwt.user.client.rpc.InvocationException;
024: import com.google.gwt.user.client.rpc.SerializationException;
025: import com.google.gwt.user.client.rpc.SerializationStreamFactory;
026: import com.google.gwt.user.client.rpc.SerializationStreamReader;
027:
028: /**
029: * Adapter from a {@link RequestCallback} interface to an {@link AsyncCallback}
030: * interface.
031: *
032: * For internal use only.
033: *
034: * @param <T> the type parameter for the {@link AsyncCallback}
035: */
036: public class RequestCallbackAdapter<T> implements RequestCallback {
037: /**
038: * Enumeration used to read specific return types out of a
039: * {@link SerializationStreamReader}.
040: */
041: public enum ResponseReader {
042: BOOLEAN {
043: @Override
044: public Object read(SerializationStreamReader streamReader)
045: throws SerializationException {
046: return streamReader.readBoolean();
047: }
048: },
049:
050: BYTE {
051: @Override
052: public Object read(SerializationStreamReader streamReader)
053: throws SerializationException {
054: return streamReader.readByte();
055: }
056: },
057:
058: CHAR {
059: @Override
060: public Object read(SerializationStreamReader streamReader)
061: throws SerializationException {
062: return streamReader.readChar();
063: }
064: },
065:
066: DOUBLE {
067: @Override
068: public Object read(SerializationStreamReader streamReader)
069: throws SerializationException {
070: return streamReader.readDouble();
071: }
072: },
073:
074: FLOAT {
075: @Override
076: public Object read(SerializationStreamReader streamReader)
077: throws SerializationException {
078: return streamReader.readFloat();
079: }
080: },
081:
082: INT {
083: @Override
084: public Object read(SerializationStreamReader streamReader)
085: throws SerializationException {
086: return streamReader.readInt();
087: }
088: },
089:
090: LONG {
091: @Override
092: public Object read(SerializationStreamReader streamReader)
093: throws SerializationException {
094: return streamReader.readLong();
095: }
096: },
097:
098: OBJECT {
099: @Override
100: public Object read(SerializationStreamReader streamReader)
101: throws SerializationException {
102: return streamReader.readObject();
103: }
104: },
105:
106: SHORT {
107: @Override
108: public Object read(SerializationStreamReader streamReader)
109: throws SerializationException {
110: return streamReader.readShort();
111: }
112: },
113:
114: STRING {
115: @Override
116: public Object read(SerializationStreamReader streamReader)
117: throws SerializationException {
118: return streamReader.readString();
119: }
120: },
121:
122: VOID {
123: @Override
124: public Object read(SerializationStreamReader streamReader) {
125: return null;
126: }
127: };
128:
129: public abstract Object read(
130: SerializationStreamReader streamReader)
131: throws SerializationException;
132: }
133:
134: /**
135: * {@link AsyncCallback} to notify or success or failure.
136: */
137: private final AsyncCallback<T> callback;
138:
139: /**
140: * Instance which will read the expected return type out of the
141: * {@link SerializationStreamReader}.
142: */
143: private final ResponseReader responseReader;
144:
145: /**
146: * {@link SerializationStreamFactory} for creating
147: * {@link SerializationStreamReader}s.
148: */
149: private final SerializationStreamFactory streamFactory;
150:
151: public RequestCallbackAdapter(
152: SerializationStreamFactory streamFactory,
153: AsyncCallback<T> callback, ResponseReader responseReader) {
154: assert (streamFactory != null);
155: assert (callback != null);
156: assert (responseReader != null);
157:
158: this .streamFactory = streamFactory;
159: this .callback = callback;
160: this .responseReader = responseReader;
161: }
162:
163: public void onError(Request request, Throwable exception) {
164: callback.onFailure(exception);
165: }
166:
167: @SuppressWarnings("unchecked")
168: public void onResponseReceived(Request request, Response response) {
169: T result = null;
170: Throwable caught = null;
171: try {
172: String encodedResponse = response.getText();
173:
174: if (RemoteServiceProxy.isReturnValue(encodedResponse)) {
175: result = (T) responseReader.read(streamFactory
176: .createStreamReader(encodedResponse));
177: } else if (RemoteServiceProxy
178: .isThrownException(encodedResponse)) {
179: caught = (Throwable) streamFactory.createStreamReader(
180: encodedResponse).readObject();
181: } else {
182: caught = new InvocationException(encodedResponse);
183: }
184: } catch (com.google.gwt.user.client.rpc.SerializationException e) {
185: caught = new IncompatibleRemoteServiceException();
186: } catch (Throwable e) {
187: caught = e;
188: }
189:
190: if (caught == null) {
191: callback.onSuccess(result);
192: } else {
193: callback.onFailure(caught);
194: }
195: }
196: }
|