/**
* Copyright 2011 Colin Alworth
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.acme.gwt.server;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.logging.Logger;
import com.acme.gwt.client.TvGuideRequestFactory;
import com.acme.gwt.data.TvViewer;
import com.acme.gwt.shared.TvViewerProxy;
import com.google.gwt.user.server.Base64Utils;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.web.bindery.autobean.shared.AutoBean;
import com.google.web.bindery.autobean.shared.AutoBeanCodex;
import com.google.web.bindery.autobean.shared.AutoBeanUtils;
import com.google.web.bindery.autobean.shared.Splittable;
import com.google.web.bindery.requestfactory.server.ServiceLayer;
import com.google.web.bindery.requestfactory.server.SimpleRequestProcessor;
import com.google.web.bindery.requestfactory.shared.DefaultProxyStore;
import com.google.web.bindery.requestfactory.shared.impl.MessageFactoryHolder;
import com.google.web.bindery.requestfactory.shared.messages.IdMessage;
import com.google.web.bindery.requestfactory.shared.messages.IdMessage.Strength;
import com.google.web.bindery.requestfactory.shared.messages.InvocationMessage;
import com.google.web.bindery.requestfactory.shared.messages.MessageFactory;
import com.google.web.bindery.requestfactory.shared.messages.OperationMessage;
import com.google.web.bindery.requestfactory.shared.messages.RequestMessage;
import com.google.web.bindery.requestfactory.shared.messages.ResponseMessage;
import com.google.web.bindery.requestfactory.vm.impl.OperationKey;
/**
* @author colin
*
*/
public class TvViewerJsonBootstrap {
@Inject
InjectableServiceLayerDecorator isld;
@Inject
Provider<TvViewer> viewerProvider;
@Inject
Logger logger;
public String getViewerAsJson() {
TvViewer user = viewerProvider.get();
if (user == null) {
return "";
}
SimpleRequestProcessor p = new SimpleRequestProcessor(ServiceLayer.create(isld));
MessageFactory factory = MessageFactoryHolder.FACTORY;
AutoBean<IdMessage> id = factory.id();
try {
id.as().setServerId(
Base64Utils.toBase64(("\"" + user.getId().toString() + "\"").getBytes("UTF-8")));
} catch (UnsupportedEncodingException e) {
return "";
}
id.as().setStrength(Strength.PERSISTED);
id.as().setTypeToken(OperationKey.hash(TvViewerProxy.class.getName()));
AutoBean<RequestMessage> msg = factory.request();
msg.as().setInvocations(new ArrayList<InvocationMessage>());
InvocationMessage invocation = factory.invocation().as();
invocation.setOperation("?");
invocation.setParameters(new ArrayList<Splittable>());
invocation.getParameters().add(AutoBeanCodex.encode(id));
msg.as().getInvocations().add(invocation);
msg.as().setRequestFactory(TvGuideRequestFactory.class.getName());
String value = p.process(AutoBeanCodex.encode(msg).getPayload());
ResponseMessage response = AutoBeanCodex.decode(factory,
ResponseMessage.class, value).as();
OperationMessage opMsg = response.getOperations().get(0);
DefaultProxyStore store = new DefaultProxyStore();
store.put(TvViewerProxy.STORE_KEY, AutoBeanCodex.encode(AutoBeanUtils
.getAutoBean(opMsg)));
return store.encode();
}
}