Example usage for org.apache.http.entity InputStreamEntity setContentType

List of usage examples for org.apache.http.entity InputStreamEntity setContentType

Introduction

In this page you can find the example usage for org.apache.http.entity InputStreamEntity setContentType.

Prototype

public void setContentType(Header header) 

Source Link

Usage

From source file:opendial.plugins.NuanceSpeech.java

/**
 * Processes the audio data contained in tempFile (based on the recognition
 * grammar whenever provided) and updates the dialogue state with the new user
 * inputs.//from w w  w.j  a v a 2  s  .  c  o  m
 * 
 * @param stream the speech stream containing the audio data
 */
private void recognise(SpeechData stream) {

    int sampleRate = (int) stream.getFormat().getSampleRate();
    log.fine("calling Nuance server for recognition... " + "(sample rate: " + sampleRate + " Hz.)");
    try {

        HttpPost httppost = new HttpPost(asrURI);
        String format = "audio/x-wav;codec=pcm;bit=" + stream.getFormat().getFrameSize() * 8 + ";rate="
                + sampleRate;
        String lang = system.getSettings().params.getProperty("lang");
        httppost.addHeader("Content-Type", format);
        httppost.addHeader("Accept", "application/xml");
        httppost.addHeader("Accept-Language", lang);
        InputStreamEntity reqEntity = new InputStreamEntity(stream);
        reqEntity.setContentType(format);
        httppost.setEntity(reqEntity);

        HttpResponse response = asrClient.execute(httppost);
        HttpEntity resEntity = response.getEntity();
        if (resEntity == null) {
            log.warning("Response entity is null, aborting");
        }

        BufferedReader reader = new BufferedReader(new InputStreamReader(resEntity.getContent()));

        if (response.getStatusLine().getStatusCode() != 200) {
            log.warning(
                    "(speech could not be recognised: error " + response.getStatusLine().getStatusCode() + ")");
            String sentence;
            while ((sentence = reader.readLine()) != null) {
                log.warning(sentence);
            }
        } else {

            String sentence;
            Map<String, Double> lines = new HashMap<String, Double>();
            while ((sentence = reader.readLine()) != null) {
                lines.put(sentence, 1.0 / (lines.size() + 1));
            }
            lines = InferenceUtils.normalise(lines);
            for (String s : new ArrayList<String>(lines.keySet())) {
                lines.put(s, ((int) (lines.get(s) * 100)) / 100.0);
            }

            log.fine("recognition results: " + lines);
            reader.close();
            if (!lines.isEmpty()) {
                system.addUserInput(lines);
            }
        }
        httppost.releaseConnection();
    } catch (Exception e) {
        log.warning("could not extract ASR results: " + e);
    }
}

From source file:org.dataconservancy.access.connector.HttpDcsConnector.java

@Override
public String uploadFile(InputStream is, long length) throws DcsClientFault {
    HttpPost post;//w  w w. j a v a2s. c o  m

    try {
        post = new HttpPost(config.getUploadFileUrl().toURI());
    } catch (URISyntaxException e) {
        final String msg = "Malformed upload file endpoint URL " + config.getUploadFileUrl() + ": "
                + e.getMessage();
        log.debug(msg, e);
        throw new DcsClientFault(msg, e);
    }

    InputStreamEntity data = new InputStreamEntity(is, length);
    data.setContentType("binary/octet-stream");
    data.setChunked(true);
    post.setEntity(data);

    HttpResponse resp = execute(post, HttpStatus.SC_ACCEPTED);

    try {
        resp.getEntity().consumeContent();
    } catch (IOException e) {
        throw new DcsClientFault("Problem releasing resources", e);
    }

    Header header = resp.getFirstHeader("X-dcs-src");

    if (header == null) {
        throw new DcsClientFault(
                "Server response missing required header X-dcs-src for post to " + config.getUploadFileUrl());
    }

    return header.getValue();
}

From source file:com.cloudant.mazha.HttpRequests.java

protected void setEntity(HttpEntityEnclosingRequestBase httpRequest, String contentType, InputStream is,
        long contentLength) {
    InputStreamEntity entity = new InputStreamEntity(is, contentLength);
    entity.setContentType(contentType);
    httpRequest.setEntity(entity);//from   w  w  w  .j  av a 2  s .c o m
}

From source file:iristk.speech.nuancecloud.JSpeexNuanceCloudRecognizerListener.java

private void initRequest() {
    try {//  ww  w.j  a v  a 2  s .  co m
        byteQueue.reset();
        encodedQueue.reset();
        JSpeexEnc encoder = new JSpeexEnc(16000);
        encoder.startEncoding(byteQueue, encodedQueue);

        List<NameValuePair> qparams = new ArrayList<NameValuePair>();
        qparams.add(new BasicNameValuePair("appId", APP_ID));
        qparams.add(new BasicNameValuePair("appKey", APP_KEY));
        qparams.add(new BasicNameValuePair("id", DEVICE_ID));
        URI uri = URIUtils.createURI("https", HOSTNAME, 443, SERVLET, URLEncodedUtils.format(qparams, "UTF-8"),
                null);
        final HttpPost httppost = new HttpPost(uri);
        httppost.addHeader("Content-Type", CODEC);
        httppost.addHeader("Content-Language", LANGUAGE);
        httppost.addHeader("Accept-Language", LANGUAGE);
        httppost.addHeader("Accept", RESULTS_FORMAT);
        httppost.addHeader("Accept-Topic", LM);
        if (nuanceAudioSource != null) {
            httppost.addHeader("X-Dictation-AudioSource", nuanceAudioSource.name());
        }
        if (cookie != null)
            httppost.addHeader("Cookie", cookie);

        InputStreamEntity reqEntity = new InputStreamEntity(encodedQueue.getInputStream(), -1);
        reqEntity.setContentType(CODEC);

        httppost.setEntity(reqEntity);

        postThread = new PostThread(httppost);

    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
}

From source file:com.intel.cosbench.client.swift.SwiftClient.java

public void storeStreamedObject(String container, String object, InputStream data, long length)
        throws IOException, SwiftException {
    SwiftResponse response = null;/* w w w  .j  av a  2  s  .  co m*/
    try {
        method = HttpClientUtil.makeHttpPut(getObjectPath(container, object));
        method.setHeader(X_AUTH_TOKEN, authToken);
        InputStreamEntity entity = new InputStreamEntity(data, length);
        if (length < 0)
            entity.setChunked(true);
        else
            entity.setChunked(false);
        entity.setContentType("application/octet-stream");
        ((HttpPut) method).setEntity(entity);
        response = new SwiftResponse(client.execute(method));
        if (response.getStatusCode() == SC_CREATED)
            return;
        if (response.getStatusCode() == SC_ACCEPTED)
            return;
        if (response.getStatusCode() == SC_NOT_FOUND)
            throw new SwiftFileNotFoundException("container not found: " + container,
                    response.getResponseHeaders(), response.getStatusLine());
        throw new SwiftException("unexpected return from server", response.getResponseHeaders(),
                response.getStatusLine());
    } finally {
        if (response != null)
            response.consumeResposeBody();
    }
}

From source file:iristk.speech.nuancecloud.NuanceCloudRecognizerListener.java

private void initRequest() {
    try {//from   www. jav  a  2  s. c  o m
        speechQueue.reset();

        InputStream inputStream;
        String codec;

        if (!(audioFormat.getSampleRate() == 8000 || audioFormat.getSampleRate() == 16000)) {
            System.err.println("Error: NuanceCloudRecognizer must get 8 or 16 Khz audio, not "
                    + audioFormat.getSampleRate());
        }

        if (useSpeex) {
            encodedQueue.reset();
            JSpeexEnc encoder = new JSpeexEnc((int) audioFormat.getSampleRate());
            encoder.startEncoding(speechQueue, encodedQueue);
            inputStream = encodedQueue.getInputStream();
            codec = "audio/x-speex;rate=" + (int) audioFormat.getSampleRate();
        } else {
            inputStream = speechQueue.getInputStream();
            codec = "audio/x-wav;codec=pcm;bit=16;rate=" + (int) audioFormat.getSampleRate();
        }

        List<NameValuePair> qparams = new ArrayList<NameValuePair>();
        qparams.add(new BasicNameValuePair("appId", license.getAppId()));
        qparams.add(new BasicNameValuePair("appKey", license.getAppKey()));
        qparams.add(new BasicNameValuePair("id", DEVICE_ID));
        URI uri = URIUtils.createURI("https", HOSTNAME, 443, SERVLET, URLEncodedUtils.format(qparams, "UTF-8"),
                null);
        final HttpPost httppost = new HttpPost(uri);
        httppost.addHeader("Content-Type", codec);
        httppost.addHeader("Content-Language", language);
        httppost.addHeader("Accept-Language", language);
        httppost.addHeader("Accept", RESULTS_FORMAT);
        httppost.addHeader("Accept-Topic", LM);
        if (nuanceAudioSource != null) {
            httppost.addHeader("X-Dictation-AudioSource", nuanceAudioSource.name());
        }
        if (cookie != null)
            httppost.addHeader("Cookie", cookie);

        InputStreamEntity reqEntity = new InputStreamEntity(inputStream, -1);
        reqEntity.setContentType(codec);

        httppost.setEntity(reqEntity);

        postThread = new PostThread(httppost);

    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
}

From source file:com.cloudant.client.org.lightcouch.CouchDbClientBase.java

/**
 * Performs a HTTP PUT request, saves an attachment.
 *
 * @return {@link Response}//ww  w.  ja va 2s  .c o m
 */
Response put(URI uri, InputStream instream, String contentType) {
    HttpResponse response = null;
    try {
        final HttpPut httpPut = new HttpPut(uri);
        final InputStreamEntity entity = new InputStreamEntity(instream, -1);
        entity.setContentType(contentType);
        httpPut.setEntity(entity);
        response = executeRequest(httpPut);
        return getResponse(response, Response.class, getGson());
    } finally {
        close(response);
    }
}

From source file:be.cytomine.client.HttpClient.java

public int post(byte[] data) throws IOException {
    log.debug("POST " + URL.toString());
    HttpPost httpPost = new HttpPost(URL.toString());
    if (isAuthByPrivateKey) {
        httpPost.setHeaders(headersArray);
    }/* ww w.jav a 2s . c o  m*/
    log.debug("Post send :" + data.length);

    InputStreamEntity reqEntity = new InputStreamEntity(new ByteArrayInputStream(data), data.length);
    reqEntity.setContentType("binary/octet-stream");
    reqEntity.setChunked(false);
    BufferedHttpEntity myEntity = null;
    try {
        myEntity = new BufferedHttpEntity(reqEntity);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        log.error(e);
    }

    httpPost.setEntity(myEntity);
    response = client.execute(targetHost, httpPost, localcontext);
    return response.getStatusLine().getStatusCode();
}

From source file:be.cytomine.client.HttpClient.java

public int put(byte[] data) throws IOException {
    log.debug("Put " + URL.getPath());
    HttpPut httpPut = new HttpPut(URL.getPath());
    if (isAuthByPrivateKey) {
        httpPut.setHeaders(headersArray);
    }//from ww  w  .j  a  v a 2 s.c  o m
    log.debug("Put send :" + data.length);

    InputStreamEntity reqEntity = new InputStreamEntity(new ByteArrayInputStream(data), data.length);
    reqEntity.setContentType("binary/octet-stream");
    reqEntity.setChunked(false);

    BufferedHttpEntity myEntity = null;
    try {
        myEntity = new BufferedHttpEntity(reqEntity);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        log.error(e);
    }

    httpPut.setEntity(myEntity);
    response = client.execute(targetHost, httpPut, localcontext);
    return response.getStatusLine().getStatusCode();
}