All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.hadoop.gateway.shell.AbstractRequest Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.hadoop.gateway.shell;

import groovy.lang.Closure;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

public abstract class AbstractRequest {

  private Hadoop session;

  protected AbstractRequest( Hadoop session ) {
    this.session = session;
  }

  protected Hadoop hadoop() {
    return session;
  }

  protected HttpResponse execute( HttpRequest request ) throws IOException {
    return session.executeNow( request );
  }

  protected URIBuilder uri( String... parts ) throws URISyntaxException {
    return new URIBuilder( session.base() + StringUtils.join( parts ) );
  }

  protected void addQueryParam( URIBuilder uri, String name, Object value ) {
    if( value != null ) {
      uri.addParameter( name, value.toString() );
    }
  }

  protected void addParam( List list, String name, String value ) {
    if( value != null ) {
      list.add( new BasicNameValuePair( name, value ) );
    }
  }

  abstract protected Callable callable();

  public T now() throws HadoopException {
    try {
      return callable().call();
    } catch( Exception e ) {
      throw new HadoopException( e );
    }
  }

  public Future later() {
    return hadoop().executeLater( callable() );
  }

  public Future later( final Closure closure ) {
    return hadoop().executeLater( new Callable() {
      @Override
      public T call() throws Exception {
        T result = callable().call();
        closure.call( result );
        return result;
      }
    } );
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy