Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2017 Comvai, s.r.o. All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.ctoolkit.restapi.client.adapter;
import org.ctoolkit.restapi.client.AuthRequest;
import org.ctoolkit.restapi.client.ClientErrorException;
import org.ctoolkit.restapi.client.PayloadRequest;
import org.ctoolkit.restapi.client.Request;
import org.ctoolkit.restapi.client.RequestCredential;
import org.ctoolkit.restapi.client.adaptee.NewExecutorAdaptee;
import org.ctoolkit.restapi.client.provider.TokenProvider;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* The NEW request implementation that delegates callback to related adapter.
*
* @author Aurel Medvegy
*/
class NewInstanceRequest
implements PayloadRequest
{
private final Class resource;
private final RestFacadeAdapter adapter;
private final NewExecutorAdaptee adaptee;
private final Object remoteRequest;
private Map params;
private Locale withLocale;
private GoogleRequestHeaders filler;
NewInstanceRequest( @Nonnull Class resource,
@Nonnull RestFacadeAdapter adapter,
@Nonnull NewExecutorAdaptee adaptee,
@Nullable Object remoteRequest )
{
this.resource = checkNotNull( resource );
this.adapter = checkNotNull( adapter );
this.adaptee = checkNotNull( adaptee );
this.remoteRequest = remoteRequest;
this.params = new HashMap<>();
this.filler = new GoogleRequestHeaders( adapter, remoteRequest );
}
@Override
public U underlying( Class type )
{
return ( U ) remoteRequest;
}
@Override
public T finish()
{
return finish( null, withLocale );
}
@Override
public T finish( @Nonnull RequestCredential credential )
{
checkNotNull( credential );
credential.populate( this.params );
return finish();
}
@Override
public T finish( @Nullable Map criteria )
{
return finish( criteria, withLocale );
}
@Override
public T finish( @Nullable Locale locale )
{
return finish( null, locale );
}
@Override
public T finish( @Nullable Map parameters, @Nullable Locale locale )
{
if ( remoteRequest == null )
{
try
{
return resource.newInstance();
}
catch ( InstantiationException | IllegalAccessException e )
{
throw new ClientErrorException( e.getMessage() );
}
}
if ( parameters != null )
{
params.putAll( parameters );
}
filler.acceptLanguage( locale );
filler.setAuthorizationIf();
return adapter.callbackNewInstance( adaptee, remoteRequest, resource, params, locale );
}
@Override
public Request configWith( @Nonnull Properties properties )
{
checkNotNull( properties );
RequestCredential.populate( properties, this.params );
return this;
}
@Override
public Request forLang( @Nonnull Locale locale )
{
this.withLocale = checkNotNull( locale );
return this;
}
@Override
public Request add( @Nonnull String name, @Nonnull Object value )
{
checkNotNull( name );
checkNotNull( value );
params.put( name, value );
return this;
}
@Override
public Request add( @Nonnull String name, @Nonnull String value )
{
checkNotNull( name );
checkNotNull( value );
params.put( name, value );
return this;
}
@Override
public Request addHeader( @Nonnull String header, @Nonnull String value )
{
checkNotNull( header );
checkNotNull( value );
filler.addHeader( header, value );
return this;
}
@Override
public Request onBehalfOf( @Nonnull Object of )
{
filler.setOnBehalfOf( of );
return this;
}
@Override
public AuthRequest authBy( @Nonnull String token )
{
checkNotNull( token, "Authorization token expected to be not null" );
filler.setTokenCreator( ( FinalTokenProvider ) () -> token );
return new AuthRequestImpl<>( this, filler );
}
@Override
public AuthRequest authBy( @Nonnull TokenProvider