Search in sources :

Example 36 with HttpEntityEnclosingRequestBase

use of org.apache.http.client.methods.HttpEntityEnclosingRequestBase in project restfulie-java by caelum.

the class ApacheDispatcher method process.

public Response process(Request details, String method, URI uri, final Object payload) {
    if (payload == null) {
        return access(details, method, uri);
    }
    final Map<String, String> headers = details.getHeaders();
    if (!headers.containsKey("Content-type")) {
        throw new RestfulieException("You should set a content type prior to sending some payload.");
    }
    StringWriter writer = new StringWriter();
    String type = headers.get("Content-type");
    try {
        type = type.split(";")[0];
        handlerFor(type).marshal(payload, writer, client);
        writer.flush();
        HttpEntityEnclosingRequestBase verb = (HttpEntityEnclosingRequestBase) verbFor(method, uri);
        add(verb, headers);
        String string = writer.getBuffer().toString();
        verb.setEntity(new StringEntity(string));
        return execute(details, verb);
    } catch (IOException e) {
        throw new RestfulieException("Unable to marshal entity.", e);
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) StringWriter(java.io.StringWriter) RestfulieException(br.com.caelum.restfulie.RestfulieException) IOException(java.io.IOException)

Example 37 with HttpEntityEnclosingRequestBase

use of org.apache.http.client.methods.HttpEntityEnclosingRequestBase in project cdap by caskdata.

the class AppFabricTestBase method addArtifact.

// add an artifact and return the response code
protected HttpResponse addArtifact(Id.Artifact artifactId, InputSupplier<? extends InputStream> artifactContents, Set<ArtifactRange> parents) throws Exception {
    String path = getVersionedAPIPath("artifacts/" + artifactId.getName(), artifactId.getNamespace().getId());
    HttpEntityEnclosingRequestBase request = getPost(path);
    request.setHeader(Constants.Gateway.API_KEY, "api-key-example");
    request.setHeader("Artifact-Version", artifactId.getVersion().getVersion());
    if (parents != null && !parents.isEmpty()) {
        request.setHeader("Artifact-Extends", Joiner.on('/').join(parents));
    }
    request.setEntity(new ByteArrayEntity(ByteStreams.toByteArray(artifactContents)));
    return execute(request);
}
Also used : HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity)

Example 38 with HttpEntityEnclosingRequestBase

use of org.apache.http.client.methods.HttpEntityEnclosingRequestBase in project cdap by caskdata.

the class AppFabricTestBase method deploy.

/**
   * Deploys an application with (optionally) a defined app name and app version
   */
protected HttpResponse deploy(Class<?> application, @Nullable String apiVersion, @Nullable String namespace, @Nullable String artifactVersion, @Nullable Config appConfig, @Nullable String ownerPrincipal) throws Exception {
    namespace = namespace == null ? Id.Namespace.DEFAULT.getId() : namespace;
    apiVersion = apiVersion == null ? Constants.Gateway.API_VERSION_3_TOKEN : apiVersion;
    artifactVersion = artifactVersion == null ? String.format("1.0.%d", System.currentTimeMillis()) : artifactVersion;
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(ManifestFields.BUNDLE_VERSION, artifactVersion);
    File artifactJar = buildAppArtifact(application, application.getSimpleName(), manifest);
    File expandDir = tmpFolder.newFolder();
    BundleJarUtil.unJar(Locations.toLocation(artifactJar), expandDir);
    // Add webapp
    File webAppFile = new File(expandDir, "webapp/default/netlens/src/1.txt");
    webAppFile.getParentFile().mkdirs();
    Files.write("dummy data", webAppFile, Charsets.UTF_8);
    BundleJarUtil.createJar(expandDir, artifactJar);
    HttpEntityEnclosingRequestBase request;
    String versionedApiPath = getVersionedAPIPath("apps/", apiVersion, namespace);
    request = getPost(versionedApiPath);
    request.setHeader(Constants.Gateway.API_KEY, "api-key-example");
    request.setHeader(AbstractAppFabricHttpHandler.ARCHIVE_NAME_HEADER, String.format("%s-%s.jar", application.getSimpleName(), artifactVersion));
    if (appConfig != null) {
        request.setHeader(AbstractAppFabricHttpHandler.APP_CONFIG_HEADER, GSON.toJson(appConfig));
    }
    if (ownerPrincipal != null) {
        request.setHeader(AbstractAppFabricHttpHandler.PRINCIPAL_HEADER, ownerPrincipal);
    }
    request.setEntity(new FileEntity(artifactJar));
    return execute(request);
}
Also used : HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) FileEntity(org.apache.http.entity.FileEntity) Manifest(java.util.jar.Manifest) File(java.io.File)

Aggregations

HttpEntityEnclosingRequestBase (org.apache.http.client.methods.HttpEntityEnclosingRequestBase)38 HttpPost (org.apache.http.client.methods.HttpPost)17 IOException (java.io.IOException)9 HttpPut (org.apache.http.client.methods.HttpPut)9 HttpEntity (org.apache.http.HttpEntity)7 HttpResponse (org.apache.http.HttpResponse)6 InputStream (java.io.InputStream)5 Header (org.apache.http.Header)5 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)5 NameValuePair (org.apache.http.NameValuePair)4 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)4 StringEntity (org.apache.http.entity.StringEntity)4 File (java.io.File)3 HashMap (java.util.HashMap)3 HttpDelete (org.apache.http.client.methods.HttpDelete)3 HttpGet (org.apache.http.client.methods.HttpGet)3 FileEntity (org.apache.http.entity.FileEntity)3 OutputStream (java.io.OutputStream)2 URI (java.net.URI)2 LinkedList (java.util.LinkedList)2