com.backendless.core.responder.AdaptingResponder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-sdk-common Show documentation
Show all versions of java-sdk-common Show documentation
Provides access to Backendless API
The newest version!
/*
* ********************************************************************************************************************
*
* BACKENDLESS.COM CONFIDENTIAL
*
* ********************************************************************************************************************
*
* Copyright 2012 BACKENDLESS.COM. All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains the property of Backendless.com and its suppliers,
* if any. The intellectual and technical concepts contained herein are proprietary to Backendless.com and its
* suppliers and may be covered by U.S. and Foreign Patents, patents in process, and are protected by trade secret
* or copyright law. Dissemination of this information or reproduction of this material is strictly forbidden
* unless prior written permission is obtained from Backendless.com.
*
* ********************************************************************************************************************
*/
package com.backendless.core.responder;
import com.backendless.core.responder.policy.IAdaptingPolicy;
import com.backendless.core.responder.policy.PoJoAdaptingPolicy;
import com.backendless.exceptions.BackendlessFault;
import weborb.client.Fault;
import weborb.client.IRawResponder;
import weborb.client.IResponder;
import weborb.exceptions.AdaptingException;
import weborb.reader.AnonymousObject;
import weborb.reader.NamedObject;
import weborb.reader.StringType;
import weborb.types.IAdaptingType;
import weborb.v3types.ErrMessage;
import java.util.Map;
public class AdaptingResponder implements IRawResponder
{
private Class clazz;
private IResponder nextResponder;
private IAdaptingPolicy adaptingPolicy;
public AdaptingResponder()
{
this.adaptingPolicy = new PoJoAdaptingPolicy<>();
}
public AdaptingResponder( IAdaptingPolicy adaptingPolicy )
{
this.adaptingPolicy = adaptingPolicy;
}
public AdaptingResponder( Class clazz )
{
this.clazz = clazz;
this.adaptingPolicy = new PoJoAdaptingPolicy<>();
}
public AdaptingResponder( Class clazz, IAdaptingPolicy adaptingPolicy )
{
this.clazz = clazz;
this.adaptingPolicy = adaptingPolicy;
}
public AdaptingResponder( Class clazz, IAdaptingPolicy adaptingPolicy, IResponder nextResponder )
{
this.clazz = clazz;
this.nextResponder = nextResponder;
this.adaptingPolicy = adaptingPolicy;
}
public final void responseHandler( Object adaptingType )
{
IAdaptingType type = (IAdaptingType) adaptingType;
IAdaptingType bodyHolder = ((NamedObject) type).getTypedObject();
if( ((IAdaptingType) adaptingType).getDefaultType().equals( ErrMessage.class ) )
{
if( nextResponder != null )
{
nextResponder.errorHandler( adaptFault( (AnonymousObject) bodyHolder ) );
}
}
else
{
IAdaptingType entity = (IAdaptingType) ((AnonymousObject) bodyHolder).getProperties().get( "body" );
try
{
adaptingPolicy.adapt( clazz, entity, nextResponder );
}
catch( AdaptingException e )
{
errorHandler( new BackendlessFault( e ) );
}
}
}
public void errorHandler( Fault fault )
{
nextResponder.errorHandler( fault );
}
public void setNextResponder( IResponder nextResponder )
{
this.nextResponder = nextResponder;
}
IResponder getNextResponder()
{
return nextResponder;
}
Class getClazz()
{
return clazz;
}
public static BackendlessFault adaptFault( AnonymousObject bodyHolder )
{
final StringType faultMessage = (StringType) bodyHolder.getProperties().get( "faultString" );
final StringType faultDetail = (StringType) bodyHolder.getProperties().get( "faultDetail" );
final StringType faultCode = (StringType) bodyHolder.getProperties().get( "faultCode" );
final AnonymousObject extendedData = (AnonymousObject) bodyHolder.getProperties().get( "extendedData" );
return new BackendlessFault( new Fault( (String) faultMessage.defaultAdapt(),
(String) faultDetail.defaultAdapt(),
(String) faultCode.defaultAdapt() ),
(Map) extendedData.defaultAdapt() );
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy