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.logging.Logger;
022
023 import org.fusesource.restygwt.client.Method;
024 import org.fusesource.restygwt.client.ModelChange;
025 import org.fusesource.restygwt.example.client.event.ModelChangeEvent;
026
027 import com.google.gwt.core.client.GWT;
028 import com.google.gwt.event.shared.EventBus;
029 import com.google.gwt.http.client.RequestCallback;
030 import com.google.gwt.http.client.Response;
031 import com.google.gwt.json.client.JSONArray;
032 import com.google.gwt.json.client.JSONParser;
033 import com.google.gwt.json.client.JSONValue;
034 import com.google.gwt.logging.client.LogConfiguration;
035
036 public class ModelChangeCallbackFilter implements CallbackFilter {
037
038 protected EventBus eventBus;
039
040 public ModelChangeCallbackFilter(EventBus eventBus) {
041 this.eventBus = eventBus;
042 }
043
044 /**
045 * the real filter method, called independent of the response code
046 *
047 * TODO method.getResponse() is not equal to response. unfortunately
048 */
049 @Override
050 public RequestCallback filter(final Method method, final Response response,
051 RequestCallback callback) {
052 final int code = response.getStatusCode();
053
054 if (code < Response.SC_MULTIPLE_CHOICES // code < 300
055 && code >= Response.SC_OK) { // code >= 200
056 String modelChangeIdentifier = method.getData().get(
057 ModelChange.MODEL_CHANGED_DOMAIN_KEY);
058
059 if (modelChangeIdentifier != null) {
060 GWT.log("found modelChangeIdentifier \"" + modelChangeIdentifier + "\" in "
061 + response);
062 JSONValue jsonValue = JSONParser.parseStrict(modelChangeIdentifier);
063 JSONArray jsonArray = jsonValue.isArray();
064
065 if (jsonArray != null) {
066 for (int i = 0; i < jsonArray.size(); ++i) {
067 ModelChangeEvent e = new ModelChangeEvent(
068 jsonArray.get(i).isString().stringValue());
069
070 if (LogConfiguration.loggingIsEnabled()) {
071 Logger.getLogger(ModelChangeCallbackFilter.class.getName())
072 .info("fire event \"" + e + "\" ...");
073 }
074 eventBus.fireEvent(e);
075 }
076 } else {
077 if (GWT.isClient() && LogConfiguration.loggingIsEnabled()) {
078 Logger.getLogger(ModelChangeCallbackFilter.class.getName())
079 .info("found null array for model-change events");
080 }
081 }
082 }
083 return callback;
084 }
085
086 GWT.log("no event processing due to invalid response code: " + code);
087 return callback;
088 }
089 }