public void i_execute_with_parameters( String httpMethodString,
String path,
DataTable dataTable ) throws Throwable {
HttpMethod httpMethod = HttpMethod.valueOf( StringUtils.upperCase( httpMethodString ) );
UriComponentsBuilder uri = UriComponentsBuilder.fromUriString( restUrlBuilder.build( path ) );
HttpHeaders headers = new HttpHeaders();
if ( Arrays.asList( HttpMethod.POST, HttpMethod.PUT ).contains( httpMethod ) ) {
headers.setContentType( MediaType.APPLICATION_FORM_URLENCODED );
}
headers.setAccept( Arrays.asList( MediaType.APPLICATION_JSON ) );
Authentication defaultAuthentication = context.lookup( "rest.base.auth" );
if ( defaultAuthentication != null ) {
LOG.debug( "applying default request authentication {}", defaultAuthentication );
defaultAuthentication.apply( uri, headers );
}
MultiValueMap<String, Object> values = new LinkedMultiValueMap<String, Object>();
if ( dataTable != null ) {
DataTable cleanedTable = fixTableHeader( dataTable );
boolean hasTypeSpec = cleanedTable.topCells().size() > 2;
for ( Map<String, String> row : cleanedTable.asMaps( String.class, String.class ) ) {
String name = spel.getValueAsString( row.get( "name" ) );
String value = spel.getValueAsString( row.get( "value" ) );
String type = row.get( "type" );
if ( type != null ) {
type = spel.getValueAsString( type );
}
else {
type = "data";
}
// Content-type is a special case
if ( StringUtils.startsWithIgnoreCase( type, "query" ) ) {
uri.queryParam( name, value );
}
else if ( StringUtils.equalsIgnoreCase( type, "header" ) ) {
if ( StringUtils.equalsIgnoreCase( "content-type", name ) ) {
headers.setContentType( MediaType.valueOf( value ) );
}
else {
headers.add( name, value );
}
}
else if ( StringUtils.startsWithIgnoreCase( type, "auth" )
|| ( !hasTypeSpec
&& ( StringUtils.equals( name, "auth" ) || StringUtils.equals( name, "authentication" ) ) )
) {
Authentication authentication = context.lookup( value );
if ( authentication == null ) {
fail( "No authentication configured under variable " + value );
}
LOG.debug( "applying request authentication {}", authentication );
authentication.apply( uri, headers );
}
else if ( StringUtils.equalsIgnoreCase( "content-type", name ) ) {
headers.setContentType( MediaType.valueOf( value ) );
}
else if ( StringUtils.equalsIgnoreCase( type, "data" ) ) {
switch ( httpMethod ) {
case POST:
case PUT:
values.add( name, value );
break;
default:
uri.queryParam( name, value );
break;
}
}
else if ( StringUtils.equalsIgnoreCase( type, "resource" ) ) {
final FileResource fileResource = fileResourceResolver.resolve( value );
switch ( httpMethod ) {
case POST:
case PUT:
values.add( name, new ByteArrayResource( fileResource.getContent() )
{
@Override
public String getFilename() {
return fileResource.getFileName();
}
} );
break;
default:
fail( "Parameter type resource is only usable with POST and PUT" );
}
}
else {
fail( "Unknown parameter type: " + type );
}
}
}
HttpEntity<?> request;
switch ( httpMethod ) {
case POST:
case PUT:
request = new HttpEntity<MultiValueMap<?, ?>>( values, headers );
break;
default:
request = new HttpEntity<String>( headers );
}
restRequest.execute(
uri.build().toUriString(),
httpMethod,
request
);
}