Example usage for org.apache.http.entity ContentType getOrDefault

List of usage examples for org.apache.http.entity ContentType getOrDefault

Introduction

In this page you can find the example usage for org.apache.http.entity ContentType getOrDefault.

Prototype

public static ContentType getOrDefault(HttpEntity httpEntity)
            throws ParseException, UnsupportedCharsetException 

Source Link

Usage

From source file:org.archive.modules.fetcher.FetchHTTP.java

/**
 * Set the character encoding based on the result headers or default.
 * //from www.  j a  va 2s .c  om
 * The HttpClient returns its own default encoding ("ISO-8859-1") if one
 * isn't specified in the Content-Type response header. We give the user the
 * option of overriding this, so we need to detect the case where the
 * default is returned.
 * 
 * Now, it may well be the case that the default returned by HttpClient and
 * the default defined by the user are the same.
 * 
 * TODO:FIXME?: This method does not do the "detect the case where the
 * [HttpClient] default is returned" mentioned above! Why not?
 * 
 * @param rec
 *            Recorder for this request.
 * @param response
 *            Method used for the request.
 */
protected void setCharacterEncoding(CrawlURI curi, final Recorder rec, final HttpResponse response) {
    rec.setCharset(getDefaultCharset());
    try {
        Charset charset = ContentType.getOrDefault(response.getEntity()).getCharset();
        if (charset != null) {
            rec.setCharset(charset);
        }
    } catch (IllegalArgumentException e) {
        // exception could be UnsupportedCharsetException or IllegalCharsetNameException
        String unsatisfiableCharset;
        try {
            unsatisfiableCharset = response.getFirstHeader("content-type").getElements()[0]
                    .getParameterByName("charset").getValue();
        } catch (Exception f) {
            unsatisfiableCharset = "<failed-to-parse>";
        }
        curi.getAnnotations()
                .add("unsatisfiableCharsetInHeader:" + StringUtils.stripToEmpty(unsatisfiableCharset));
    }
}

From source file:org.apache.manifoldcf.elasticsearch.MCFAuthorizerUtils.java

/** Get access tokens given a username */
protected static List<String> getAccessTokens(Map<String, String> domainMap) throws MCFAuthorizerException {
    try {/*from w  ww . ja  v a2s. co m*/
        StringBuilder urlBuffer = new StringBuilder(AUTHORITY_BASE_URL);
        urlBuffer.append("/UserACLs");
        int i = 0;
        for (String domain : domainMap.keySet()) {
            if (i == 0)
                urlBuffer.append("?");
            else
                urlBuffer.append("&");
            // For backwards compatibility, handle the singleton case specially
            if (domainMap.size() == 1 && domain.length() == 0) {
                urlBuffer.append("username=").append(URLEncoder.encode(domainMap.get(domain), "utf-8"));
            } else {
                urlBuffer.append("username_").append(Integer.toString(i)).append("=")
                        .append(URLEncoder.encode(domainMap.get(domain), "utf-8")).append("&").append("domain_")
                        .append(Integer.toString(i)).append("=").append(URLEncoder.encode(domain, "utf-8"));
            }
            i++;
        }
        String theURL = urlBuffer.toString();
        HttpGet method = new HttpGet(theURL);
        try {
            HttpResponse httpResponse = httpClient.execute(method);
            int rval = httpResponse.getStatusLine().getStatusCode();
            if (rval != 200) {
                String response = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
                throw new MCFAuthorizerException(
                        "Couldn't fetch user's access tokens from ManifoldCF authority service: "
                                + Integer.toString(rval) + "; " + response);
            }
            InputStream is = httpResponse.getEntity().getContent();
            try {
                String charSet = ContentType.getOrDefault(httpResponse.getEntity()).getCharset().toString();
                if (charSet == null)
                    charSet = "utf-8";
                Reader r = new InputStreamReader(is, charSet);
                try {
                    BufferedReader br = new BufferedReader(r);
                    try {
                        // Read the tokens, one line at a time.  If any authorities are down, we have no current way to note that, but someday we will.
                        List<String> tokenList = new ArrayList<String>();
                        while (true) {
                            String line = br.readLine();
                            if (line == null)
                                break;
                            if (line.startsWith("TOKEN:")) {
                                tokenList.add(line.substring("TOKEN:".length()));
                                log.info(line);
                            } else {
                                // It probably says something about the state of the authority(s) involved, so log it
                                log.info("Saw authority response " + line);
                            }
                        }
                        return tokenList;
                    } finally {
                        br.close();
                    }
                } finally {
                    r.close();
                }
            } finally {
                is.close();
            }
        } finally {
            method.abort();
        }
    } catch (IOException e) {
        throw new MCFAuthorizerException("IO exception: " + e.getMessage(), e);
    }
}

From source file:org.cloudcoder.app.server.rpc.GetCoursesAndProblemsServiceImpl.java

@Override
public ProblemAndTestCaseList importExercise(Course course, String exerciseHash)
        throws CloudCoderAuthenticationException {
    if (course == null || exerciseHash == null) {
        throw new IllegalArgumentException();
    }//from   w w w. j a  v a 2  s  . com

    // Make sure a user is authenticated
    User user = ServletUtil.checkClientIsAuthenticated(getThreadLocalRequest(),
            GetCoursesAndProblemsServiceImpl.class);

    // Find user's registration in the course: if user is not instructor,
    // import is not allowed
    CourseRegistrationList reg = Database.getInstance().findCourseRegistrations(user, course);
    if (!reg.isInstructor()) {
        throw new CloudCoderAuthenticationException("Only an instructor can import a problem in a course");
    }

    // Attempt to load the problem from the exercise repository.
    ConfigurationSetting repoUrlSetting = Database.getInstance()
            .getConfigurationSetting(ConfigurationSettingName.PUB_REPOSITORY_URL);
    if (repoUrlSetting == null) {
        logger.error("Repository URL configuration setting is not set");
        return null;
    }

    // GET the exercise from the repository
    HttpGet get = new HttpGet(repoUrlSetting.getValue() + "/exercisedata/" + exerciseHash);
    ProblemAndTestCaseList exercise = null;

    HttpClient client = new DefaultHttpClient();
    try {
        HttpResponse response = client.execute(get);

        HttpEntity entity = response.getEntity();

        ContentType contentType = ContentType.getOrDefault(entity);
        Reader reader = new InputStreamReader(entity.getContent(), contentType.getCharset());

        exercise = new ProblemAndTestCaseList();
        exercise.setTestCaseList(new TestCase[0]);
        JSONConversion.readProblemAndTestCaseData(exercise, ReflectionFactory.forClass(Problem.class),
                ReflectionFactory.forClass(TestCase.class), reader);

        // Set the course id
        exercise.getProblem().setCourseId(course.getId());
    } catch (IOException e) {
        logger.error("Error importing exercise from repository", e);
        return null;
    } finally {
        client.getConnectionManager().shutdown();
    }

    // Set "when assigned" and "when due" to reasonable default values
    long now = System.currentTimeMillis();
    exercise.getProblem().setWhenAssigned(now);
    exercise.getProblem().setWhenDue(now + 48L * 60L * 60L * 1000L);

    // Set problem authorship as IMPORTED
    exercise.getProblem().setProblemAuthorship(ProblemAuthorship.IMPORTED);

    // For IMPORTED problems, parent_hash is actually the hash of the problem
    // itself.  If the problem is modified (and the authorship changed
    // to IMPORTED_AND_MODIFIED), then the (unchanged) parent_hash value
    // really does reflect the "parent" problem.
    exercise.getProblem().setParentHash(exerciseHash);

    // Store the exercise in the database
    exercise = Database.getInstance().storeProblemAndTestCaseList(exercise, course, user);

    return exercise;
}

From source file:sk.datalan.solr.impl.HttpSolrServer.java

protected NamedList<Object> executeMethod(HttpRequestBase method, final ResponseParser processor)
        throws SolrServerException {
    //    // XXX client already has this set, is this needed?
    //    method.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS,
    //        followRedirects);
    method.addHeader("User-Agent", AGENT);

    InputStream respBody = null;//w  w  w .j  a va2s  .  c o m
    boolean shouldClose = true;
    boolean success = false;
    try {
        // Execute the method.
        final HttpResponse response = httpClient.execute(method);
        int httpStatus = response.getStatusLine().getStatusCode();

        // Read the contents
        respBody = response.getEntity().getContent();
        Header ctHeader = response.getLastHeader("content-type");
        String contentType;
        if (ctHeader != null) {
            contentType = ctHeader.getValue();
        } else {
            contentType = "";
        }

        // handle some http level checks before trying to parse the response
        switch (httpStatus) {
        case HttpStatus.SC_OK:
        case HttpStatus.SC_BAD_REQUEST:
        case HttpStatus.SC_CONFLICT: // 409
            break;
        case HttpStatus.SC_MOVED_PERMANENTLY:
        case HttpStatus.SC_MOVED_TEMPORARILY:
            if (!followRedirects) {
                throw new SolrServerException(
                        "Server at " + getBaseURL() + " sent back a redirect (" + httpStatus + ").");
            }
            break;
        default:
            if (processor == null) {
                throw new RemoteSolrException(httpStatus,
                        "Server at " + getBaseURL() + " returned non ok status:" + httpStatus + ", message:"
                                + response.getStatusLine().getReasonPhrase(),
                        null);
            }
        }
        if (processor == null) {

            // no processor specified, return raw stream
            NamedList<Object> rsp = new NamedList<>();
            rsp.add("stream", respBody);
            // Only case where stream should not be closed
            shouldClose = false;
            success = true;
            return rsp;
        }

        String procCt = processor.getContentType();
        if (procCt != null) {
            String procMimeType = ContentType.parse(procCt).getMimeType().trim().toLowerCase(Locale.ROOT);
            String mimeType = ContentType.parse(contentType).getMimeType().trim().toLowerCase(Locale.ROOT);
            if (!procMimeType.equals(mimeType)) {
                // unexpected mime type
                String msg = "Expected mime type " + procMimeType + " but got " + mimeType + ".";
                Header encodingHeader = response.getEntity().getContentEncoding();
                String encoding;
                if (encodingHeader != null) {
                    encoding = encodingHeader.getValue();
                } else {
                    encoding = "UTF-8"; // try UTF-8
                }
                try {
                    msg = msg + " " + IOUtils.toString(respBody, encoding);
                } catch (IOException e) {
                    throw new RemoteSolrException(httpStatus,
                            "Could not parse response with encoding " + encoding, e);
                }
                RemoteSolrException e = new RemoteSolrException(httpStatus, msg, null);
                throw e;
            }
        }

        //      if(true) {
        //        ByteArrayOutputStream copy = new ByteArrayOutputStream();
        //        IOUtils.copy(respBody, copy);
        //        String val = new String(copy.toByteArray());
        //        System.out.println(">RESPONSE>"+val+"<"+val.length());
        //        respBody = new ByteArrayInputStream(copy.toByteArray());
        //      }
        NamedList<Object> rsp = null;
        ContentType ct = ContentType.getOrDefault(response.getEntity());
        try {
            rsp = processor.processResponse(respBody, ct.getCharset().toString());
        } catch (Exception e) {
            throw new RemoteSolrException(httpStatus, e.getMessage(), e);
        }
        if (httpStatus != HttpStatus.SC_OK) {
            String reason = null;
            try {
                NamedList err = (NamedList) rsp.get("error");
                if (err != null) {
                    reason = (String) err.get("msg");
                    if (reason == null) {
                        reason = (String) err.get("trace");
                    }
                }
            } catch (Exception ex) {
            }
            if (reason == null) {
                StringBuilder msg = new StringBuilder();
                msg.append(response.getStatusLine().getReasonPhrase());
                msg.append("\n\n");
                msg.append("request: ").append(method.getURI());
                reason = java.net.URLDecoder.decode(msg.toString(), UTF_8);
            }
            throw new RemoteSolrException(httpStatus, reason, null);
        }
        success = true;
        return rsp;
    } catch (ConnectException e) {
        throw new SolrServerException("Server refused connection at: " + getBaseURL(), e);
    } catch (SocketTimeoutException e) {
        throw new SolrServerException("Timeout occured while waiting response from server at: " + getBaseURL(),
                e);
    } catch (IOException e) {
        throw new SolrServerException("IOException occured when talking to server at: " + getBaseURL(), e);
    } finally {
        if (respBody != null && shouldClose) {
            try {
                respBody.close();
            } catch (IOException e) {
                log.error("", e);
            } finally {
                if (!success) {
                    method.abort();
                }
            }
        }
    }
}

From source file:edu.lternet.pasta.client.DataPackageManagerClient.java

/**
 * Executes the 'readDataEntityName' web service method.
 * //ww w . ja v  a2 s .  c o  m
 * @param scope
 *          the scope value, e.g. "knb-lter-lno"
 * @param identifier
 *          the identifier value, e.g. 10
 * @param revision
 *          the revision value, e.g. "1"
 * @param entityId
 *          the entity identifier string, e.g. "NoneSuchBugCount"
 * @return the data entity name
 * @see <a target="top"
 *      href="http://package.lternet.edu/package/docs/api">Data Package
 *      Manager web service API</a>
 */
public String readDataEntityName(String scope, Integer identifier, String revision, String entityId)
        throws Exception {

    // Re-encode "%" to its character reference value of %25 to mitigate
    // an issue with the HttpGet call that performs the decoding - this is
    // a kludge to deal with encoding nonsense.
    entityId = entityId.replace("%", "%25");

    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    String urlTail = makeUrlTail(scope, identifier.toString(), revision, entityId);
    String url = BASE_URL + "/name/eml" + urlTail;
    HttpGet httpGet = new HttpGet(url);
    String entityString = null;

    // Set header content
    if (this.token != null) {
        httpGet.setHeader("Cookie", "auth-token=" + this.token);
    }

    try {
        HttpResponse httpResponse = httpClient.execute(httpGet);
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        HttpEntity httpEntity = httpResponse.getEntity();
        entityString = EntityUtils.toString(httpEntity);
        ContentType contentType = ContentType.getOrDefault(httpEntity);
        this.contentType = contentType.toString();
        if (statusCode != HttpStatus.SC_OK) {
            handleStatusCode(statusCode, entityString);
        }
    } finally {
        closeHttpClient(httpClient);
    }

    return entityString;
}

From source file:edu.lternet.pasta.client.DataPackageManagerClient.java

/**
 * Executes the 'readDataEntityNames' web service method.
 * /*w  w  w  . ja v  a 2s. c  om*/
 * @param scope
 *          the scope value, e.g. "knb-lter-lno"
 * @param identifier
 *          the identifier value, e.g. 10
 * @param revision
 *          the revision value, e.g. "1"
 * 
 * @return a list of data entity identifiers and their corresponding entity names,
 *         one entity per line with id and name separated by a comma. Note that
 *         many entity names themselves contain commas, so when parsing the return
 *         value, split the string only up to the first occurrence of a comma.
 */
public String readDataEntityNames(String scope, Integer identifier, String revision) throws Exception {
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    String urlTail = makeUrlTail(scope, identifier.toString(), revision, null);
    String url = BASE_URL + "/name/eml" + urlTail;
    HttpGet httpGet = new HttpGet(url);
    String entityString = null;

    // Set header content
    if (this.token != null) {
        httpGet.setHeader("Cookie", "auth-token=" + this.token);
    }

    try {
        HttpResponse httpResponse = httpClient.execute(httpGet);
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        HttpEntity httpEntity = httpResponse.getEntity();
        entityString = EntityUtils.toString(httpEntity);
        ContentType contentType = ContentType.getOrDefault(httpEntity);
        this.contentType = contentType.toString();
        if (statusCode != HttpStatus.SC_OK) {
            handleStatusCode(statusCode, entityString);
        }
    } finally {
        closeHttpClient(httpClient);
    }

    return entityString;
}

From source file:edu.lternet.pasta.client.DataPackageManagerClient.java

/**
 * Executes the 'readDataEntitySize' web service method.
 * //www . ja va2 s . co  m
 * @param scope
 *          the scope value, e.g. "knb-lter-lno"
 * @param identifier
 *          the identifier value, e.g. 10
 * @param revision
 *          the revision value, e.g. "1"
 * @param entityId
 *          the entity identifier string, e.g. "NoneSuchBugCount"
 * @return the size of the data entity resource in bytes
 * @see <a target="top"
 *      href="http://package.lternet.edu/package/docs/api">Data Package
 *      Manager web service API</a>
 */
public Long readDataEntitySize(String scope, Integer identifier, String revision, String entityId)
        throws Exception {

    // Re-encode "%" to its character reference value of %25 to mitigate
    // an issue with the HttpGet call that performs the decoding - this is
    // a kludge to deal with encoding nonsense.
    entityId = entityId.replace("%", "%25");

    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    String urlTail = makeUrlTail(scope, identifier.toString(), revision, entityId);
    String url = BASE_URL + "/data/size/eml" + urlTail;
    HttpGet httpGet = new HttpGet(url);
    Long entitySize = null;

    // Set header content
    if (this.token != null) {
        httpGet.setHeader("Cookie", "auth-token=" + this.token);
    }

    try {
        HttpResponse httpResponse = httpClient.execute(httpGet);
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        HttpEntity httpEntity = httpResponse.getEntity();
        String entityString = EntityUtils.toString(httpEntity);
        if (entityString != null) {
            try {
                entitySize = new Long(entityString);
            } catch (NumberFormatException e) {
                logger.error("Unable to determine entity size of entity: " + entityId);
            }
        }
        ContentType contentType = ContentType.getOrDefault(httpEntity);
        this.contentType = contentType.toString();
        if (statusCode != HttpStatus.SC_OK) {
            handleStatusCode(statusCode, entityString);
        }
    } finally {
        closeHttpClient(httpClient);
    }

    return entitySize;
}

From source file:edu.lternet.pasta.client.DataPackageManagerClient.java

/**
 * Executes the 'readDataEntitySizes' web service method.
 * //from  w w w . j  a  v  a  2s  .  com
 * @param scope
 *          the scope value, e.g. "knb-lter-lno"
 * @param identifier
 *          the identifier value, e.g. 10
 * @param revision
 *          the revision value, e.g. "1"
 * 
 * @return a list of data entities and their corresponding entity sizes in bytes
 */
public String readDataEntitySizes(String scope, Integer identifier, String revision) throws Exception {
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    String urlTail = makeUrlTail(scope, identifier.toString(), revision, null);
    String url = BASE_URL + "/data/size/eml" + urlTail;
    HttpGet httpGet = new HttpGet(url);
    String entityString = null;

    // Set header content
    if (this.token != null) {
        httpGet.setHeader("Cookie", "auth-token=" + this.token);
    }

    try {
        HttpResponse httpResponse = httpClient.execute(httpGet);
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        HttpEntity httpEntity = httpResponse.getEntity();
        entityString = EntityUtils.toString(httpEntity);
        ContentType contentType = ContentType.getOrDefault(httpEntity);
        this.contentType = contentType.toString();
        if (statusCode != HttpStatus.SC_OK) {
            handleStatusCode(statusCode, entityString);
        }
    } finally {
        closeHttpClient(httpClient);
    }

    return entityString;
}

From source file:org.eclipse.californium.proxy.HttpTranslator.java

/**
 * Method to map the http entity of a http message in a coherent payload for
 * the coap message. The method simply gets the bytes from the entity and,
 * if needed changes the charset of the obtained bytes to UTF-8.
 * /*ww w.  j a  va2s .c  om*/
 * @param httpEntity
 *            the http entity
 * 
 * @return byte[]
 * @throws TranslationException
 *             the translation exception
 */
public static byte[] getCoapPayload(HttpEntity httpEntity) throws TranslationException {
    if (httpEntity == null) {
        throw new IllegalArgumentException("httpEntity == null");
    }

    byte[] payload = null;
    try {
        // get the bytes from the entity
        payload = EntityUtils.toByteArray(httpEntity);
        if (payload != null && payload.length > 0 && looksLikeUTF8(payload)) {

            //modifica il payload per sostituire i riferimenti a http://proxyIP:8080/proxy/

            String body = "";
            try {
                body = new String(payload, "UTF-8");
            } catch (UnsupportedEncodingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            body = body.replace("http://" + proxyIP + ":8080/proxy/", "coap://");

            payload = body.getBytes();

            // the only supported charset in CoAP is UTF-8
            Charset coapCharset = UTF_8;

            // get the charset for the http entity
            ContentType httpContentType = ContentType.getOrDefault(httpEntity);
            Charset httpCharset = httpContentType.getCharset();

            // check if the charset is the one allowed by coap
            if (httpCharset != null && !httpCharset.equals(coapCharset)) {
                // translate the payload to the utf-8 charset
                payload = changeCharset(payload, httpCharset, coapCharset);
            }
        } else {
            int i = 0;
        }

    } catch (IOException e) {
        LOGGER.warning("Cannot get the content of the http entity: " + e.getMessage());
        throw new TranslationException("Cannot get the content of the http entity", e);
    } finally {
        try {
            // ensure all content has been consumed, so that the
            // underlying connection could be re-used
            EntityUtils.consume(httpEntity);
        } catch (IOException e) {

        }
    }

    return payload;
}