Example usage for com.google.common.net HttpHeaders ACCEPT

List of usage examples for com.google.common.net HttpHeaders ACCEPT

Introduction

In this page you can find the example usage for com.google.common.net HttpHeaders ACCEPT.

Prototype

String ACCEPT

To view the source code for com.google.common.net HttpHeaders ACCEPT.

Click Source Link

Document

The HTTP Accept header field name.

Usage

From source file:com.google.gitiles.FormatType.java

public static FormatType getFormatType(HttpServletRequest req) {
    FormatType result = (FormatType) req.getAttribute(FORMAT_TYPE_ATTRIBUTE);
    if (result != null) {
        return result;
    }/*from  w w w .  j a va  2s .c om*/

    String format = req.getParameter("format");
    if (format != null) {
        for (FormatType type : FormatType.values()) {
            if (format.equalsIgnoreCase(type.name())) {
                return set(req, type);
            }
        }
        throw new IllegalArgumentException("Invalid format " + format);
    }

    String accept = req.getHeader(HttpHeaders.ACCEPT);
    if (Strings.isNullOrEmpty(accept)) {
        return set(req, DEFAULT);
    }

    for (String p : accept.split("[ ,;][ ,;]*")) {
        for (FormatType type : FormatType.values()) {
            if (p.equals(type.mimeType)) {
                return set(req, type != HTML ? type : DEFAULT);
            }
        }
    }
    return set(req, DEFAULT);
}

From source file:org.libreoffice.ci.gerrit.buildbot.servlets.RenderType.java

public static RenderType getFormatType(HttpServletRequest req) {
    RenderType result = (RenderType) req.getAttribute(FORMAT_TYPE_ATTRIBUTE);
    if (result != null) {
        return result;
    }//from ww w  .j a va2 s .c o  m

    String format = req.getParameter("format");
    if (format != null) {
        for (RenderType type : RenderType.values()) {
            if (format.equalsIgnoreCase(type.name())) {
                return set(req, type);
            }
        }
        throw new IllegalArgumentException("Invalid format " + format);
    }

    String accept = req.getHeader(HttpHeaders.ACCEPT);
    if (Strings.isNullOrEmpty(accept)) {
        return set(req, DEFAULT);
    }

    for (String p : accept.split("[ ,;][ ,;]*")) {
        for (RenderType type : RenderType.values()) {
            if (p.equals(type.mimeType)) {
                return set(req, type != HTML ? type : DEFAULT);
            }
        }
    }
    return set(req, DEFAULT);
}

From source file:no.nb.nna.broprox.integrationtests.WarcInspector.java

public static WarcFileSet getWarcFiles() throws UncheckedIOException {
    HttpUrl url = WARC_SERVER_URL.resolve("warcs");

    Request request = new Request.Builder().url(url).header(HttpHeaders.ACCEPT, "application/json").build();

    try (Response response = CLIENT.newCall(request).execute();) {
        if (response.isSuccessful()) {
            return new WarcFileSet(
                    GSON.fromJson(response.body().charStream(), List.class).stream().map(m -> new WarcFile(m)));
        } else {/*w  w w.  ja va2  s  .c  o m*/
            throw new IOException("Unexpected code " + response);
        }
    } catch (IOException ex) {
        throw new UncheckedIOException(ex);
    }
}

From source file:org.jclouds.vcloud.director.v1_5.filters.AddAcceptHeaderToRequest.java

@Override
public HttpRequest filter(HttpRequest request) throws HttpException {
    return request.toBuilder().replaceHeader(HttpHeaders.ACCEPT, "application/*+xml;version=1.5").build();
}

From source file:be.fedict.lodtools.cpsv.EliMatcher.java

/**
 * Try to match framework using date, type and title
 * /*from w  w w  . j  a v  a2  s  .co m*/
 * @param date publication date
 * @param type document type
 * @param title title of the document
 * @return 
 */
private static Set<IRI> match(String date, String type, String title) {
    Set<IRI> matched = Collections.emptySet();

    URLConnection conn;

    try {
        String enc = URLEncoder.encode(title, StandardCharsets.UTF_8.toString());
        URL u = new URL(MessageFormat.format(ELI, date, type, enc));
        conn = u.openConnection();
        conn.setRequestProperty(HttpHeaders.ACCEPT, RDFFormat.NTRIPLES.getDefaultMIMEType());

        try (InputStream in = conn.getInputStream()) {
            Model m = Rio.parse(in, "http://pubserv.belgif.be", RDFFormat.NTRIPLES);
            matched = m.subjects().stream().map(IRI.class::cast).collect(Collectors.toSet());
            LOG.info("Mapped {}", title);
        }
    } catch (MalformedURLException ex) {
        LOG.error("Could not build url");
    } catch (FileNotFoundException ex) {
        LOG.warn("No matching ELI for {}", title);
    } catch (IOException ex) {
        LOG.error("Error matching: {}", ex);
    }
    return matched;
}

From source file:org.jclouds.rackspace.cloudidentity.v2_0.internal.RackspaceFixture.java

public HttpRequest initialAuthWithUsernameAndApiKey(String username, String apiKey) {
    return HttpRequest.builder().method("POST").endpoint("https://identity.api.rackspacecloud.com/v2.0/tokens")
            .addHeader(HttpHeaders.ACCEPT, "application/json")
            .payload(payloadFromStringWithContentType(format(
                    "{\"auth\":{\"RAX-KSKEY:apiKeyCredentials\":{\"username\":\"%s\",\"apiKey\":\"%s\"}}}",
                    username, apiKey), "application/json"))
            .build();//from   www  . j  a  v a 2  s .c om
}

From source file:org.graylog2.rest.RemoteInterfaceProvider.java

public <T> T get(Node node, final String authorizationToken, Class<T> interfaceClass) {
    final OkHttpClient okHttpClient = this.okHttpClient.newBuilder().addInterceptor(chain -> {
        final Request original = chain.request();

        Request.Builder builder = original.newBuilder()
                .header(HttpHeaders.ACCEPT, MediaType.JSON_UTF_8.toString())
                .method(original.method(), original.body());

        if (authorizationToken != null) {
            builder = builder.header(HttpHeaders.AUTHORIZATION, authorizationToken);
        }//from  w  ww. j av a2s . co m

        return chain.proceed(builder.build());
    }).build();
    final Retrofit retrofit = new Retrofit.Builder().baseUrl(node.getTransportAddress())
            .addConverterFactory(JacksonConverterFactory.create(objectMapper)).client(okHttpClient).build();

    return retrofit.create(interfaceClass);
}

From source file:org.jclouds.hpcloud.objectstorage.internal.KeystoneFixture.java

public HttpRequest initialAuthWithUsernameAndPassword(String username, String password) {
    return HttpRequest.builder().method("POST")
            .endpoint("https://region-a.geo-1.identity.hpcloudsvc.com:35357/v2.0/tokens")
            .addHeader(HttpHeaders.ACCEPT, "application/json")
            .payload(payloadFromStringWithContentType(format(
                    "{\"auth\":{\"passwordCredentials\":{\"username\":\"%s\",\"password\":\"%s\"},\"tenantName\":\"%s\"}}",
                    username, password, getTenantName()), "application/json"))
            .build();/*  w w  w .j a  v  a 2s.  com*/
}

From source file:org.jclouds.openstack.keystone.v2_0.internal.KeystoneFixture.java

public HttpRequest initialAuthWithUsernameAndPassword(String username, String password) {
    return HttpRequest.builder().method("POST").endpoint("http://localhost:5000/v2.0/tokens")
            .addHeader(HttpHeaders.ACCEPT, "application/json")
            .payload(payloadFromStringWithContentType(
                    format("{\"auth\":{\"passwordCredentials\":{\"username\":\"%s\",\"password\":\"%s\"}}}",
                            username, password),
                    "application/json"))
            .build();/*from  w  ww  .  j a v  a 2  s  . co m*/
}

From source file:com.google.gerrit.acceptance.RestSession.java

public RestResponse getJsonAccept(String endPoint) throws IOException {
    return getWithHeader(endPoint, new BasicHeader(HttpHeaders.ACCEPT, "application/json"));
}