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.callback;
020    
021    import java.util.ArrayList;
022    import java.util.List;
023    import java.util.logging.Logger;
024    
025    import org.fusesource.restygwt.client.Method;
026    
027    import com.google.gwt.core.client.GWT;
028    import com.google.gwt.http.client.Request;
029    import com.google.gwt.http.client.RequestCallback;
030    import com.google.gwt.http.client.Response;
031    import com.google.gwt.logging.client.LogConfiguration;
032    
033    public class DefaultFilterawareRequestCallback implements FilterawareRequestCallback {
034    
035        protected final Method method;
036    
037        protected RequestCallback requestCallback;
038    
039        final protected List<CallbackFilter> callbackFilters = new ArrayList<CallbackFilter>();
040    
041        public DefaultFilterawareRequestCallback(Method method) {
042            this.method = method;
043            // need to keep requestcallback here, as ``method.builder.getCallback()`` does not
044            // give the same callback later on
045            this.requestCallback = method.builder.getCallback();
046        }
047    
048        @Override
049        public void onResponseReceived(Request request, Response response) {
050            if (!(response.getStatusCode() < 300 && response.getStatusCode() >= 200)) {
051                if (GWT.isClient() && LogConfiguration.loggingIsEnabled()) {
052                    Logger.getLogger(
053                            DefaultFilterawareRequestCallback.class.getName())
054                            .severe(
055                                    "ERROR with method: "
056                                            + method.builder.getHTTPMethod() + " "
057                                            + method.builder.getUrl() + ", "
058                                            + response.getStatusText());
059                }
060    
061                /*
062                 * RuntimeException token from
063                 * com.google.gwt.http.client.Request#fireOnResponseReceived()
064                 */
065                requestCallback.onError(request, new RuntimeException("Response "
066                        + response.getStatusCode() + " for "
067                        + method.builder.getHTTPMethod() + " "
068                        + method.builder.getUrl()));
069            } else {
070                // filter only in success case for now
071                runFilters(request, response);
072            }
073        }
074    
075        protected void runFilters(Request request, Response response) {
076            for (CallbackFilter f : callbackFilters) {
077                requestCallback = f.filter(method, response, requestCallback);
078            }
079            requestCallback.onResponseReceived(request, response);
080        }
081    
082        @Override
083        public void onError(Request request, Throwable exception) {
084            requestCallback.onError(request, exception);
085        }
086    
087        /**
088         * put a filter in the "chain of responsibility" of all callbackfilters that will be
089         * performed on callback passing.
090         */
091        public void addFilter(CallbackFilter filter) {
092            callbackFilters.add(filter);
093        }
094    }