com.ning.http.client.Request Java Examples

The following examples show how to use com.ning.http.client.Request. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NotifyServiceImpl.java    From qconfig with MIT License 6 votes vote down vote up
@Override
public void notify(final String group, final String dataId, final String profile) {
    List<String> urls = getServerUrls();
    if (urls == null || urls.isEmpty()) {
        return;
    }

    String uri = this.notifyUrl;
    logger.info("notify server, group: {}, data id: {}, profile: {}, uri: {}, servers: {}", group, dataId, profile, uri, urls);
    doNotify(urls, uri, "update", new Function<String, Request>() {
        @Override
        public Request apply(String url) {
            return getRequest(url, group, dataId, profile);
        }
    });
}
 
Example #2
Source File: ParsecClientProfilingLogUtil.java    From parsec-libraries with Apache License 2.0 6 votes vote down vote up
/**
 * log remote profiling log.
 *
 * @param request ning http request
 * @param response ning http response
 * @param requestStatus request status
 * @param progress parsec async progress do
 * @param msgMap additional log msg in key=String, value=String format
 */
public static void logRemoteRequest(
        final Request request,
        final Response response,
        final String requestStatus,
        final ParsecAsyncProgress progress,
        final Map<String, String> msgMap
        ) {

    if (!PROF_LOGGER.isTraceEnabled()) {
        return;
    }

    String logMsg = formatMessage(request, response, requestStatus, progress, msgMap);
    PROF_LOGGER.trace(logMsg);
}
 
Example #3
Source File: NingEntityExtractorV1.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Override
public String getEntity(Request request) {
    final String stringData = request.getStringData();
    if (stringData != null) {
        return stringData;
    }

    final byte[] byteData = request.getByteData();
    if (byteData != null) {
        return "BYTE_DATA";
    }

    final InputStream streamData = request.getStreamData();
    if (streamData != null) {
        return "STREAM_DATA";
    }

    return null;
}
 
Example #4
Source File: AsyncHttpClientAspect1x.java    From glowroot with Apache License 2.0 6 votes vote down vote up
@OnBefore
public static @Nullable AsyncTraceEntry onBefore(ThreadContext context,
        @BindParameter @Nullable Request request,
        @BindClassMeta AsyncHttpClientRequestInvoker requestInvoker) {
    // need to start trace entry @OnBefore in case it is executed in a "same thread
    // executor" in which case will be over in @OnReturn
    if (request == null) {
        return null;
    }
    String method = request.getMethod();
    if (method == null) {
        method = "";
    } else {
        method += " ";
    }
    String url = requestInvoker.getUrl(request);
    return context.startAsyncServiceCallEntry("HTTP", method + Uris.stripQueryString(url),
            MessageSupplier.create("http client request: {}{}", method, url), timerName);
}
 
Example #5
Source File: NotifyServiceImpl.java    From qconfig with MIT License 6 votes vote down vote up
@Override
public void notifyFixedVersion(final ConfigMeta meta, final String ip, final long version) {
    List<String> serverUrls = getServerUrls();
    if (CollectionUtils.isEmpty(serverUrls)) {
        logger.warn("notify fixedVersionConsumer server, meta:{}, ip:{}, version: {}, but no server", meta, ip, version);
        return;
    }
    String uri = this.notifyFixedVersionUrl;
    logger.info("notify fixedVersionConsumer server, meta:{}, ip:{}, version: {}, servers:{}", meta, ip, version, serverUrls);
    doNotify(serverUrls, uri, "fixedVersion", new Function<String, Request>() {
        @Override
        public Request apply(String url) {
            AsyncHttpClient.BoundRequestBuilder builder = httpClient.preparePost(url);
            builder.addQueryParam(Constants.GROUP_NAME, meta.getGroup())
                    .addQueryParam(Constants.PROFILE_NAME, meta.getProfile())
                    .addQueryParam(Constants.DATAID_NAME, meta.getDataId())
                    .addQueryParam(Constants.VERSION_NAME, String.valueOf(version))
                    .addQueryParam("ip", ip)
                    .addHeader(Constants.TOKEN_NAME, ServerManager.getInstance().getAppServerConfig().getToken());
            return builder.build();
        }
    });
}
 
Example #6
Source File: NotifyServiceImpl.java    From qconfig with MIT License 6 votes vote down vote up
@Override
public void notifyPublic(final ConfigMetaWithoutProfile meta) {
    List<String> urls = getServerUrls();
    if (urls == null || urls.isEmpty()) {
        return;
    }

    String uri = this.notifyPublicUrl;
    logger.info("notify public status, {}, uri: {}, servers: {}", meta, uri, urls);
    doNotify(urls, uri, "public", new Function<String, Request>() {
        @Override
        public Request apply(String url) {
            return getRequest(url, meta.getGroup(), meta.getDataId(), "");
        }
    });
}
 
Example #7
Source File: NotifyServiceImpl.java    From qconfig with MIT License 6 votes vote down vote up
@Override
public void notifyReference(final Reference reference, final RefChangeType changeType) {
    List<String> urls = getServerUrls();
    if (urls.isEmpty()) {
        return;
    }

    String uri = this.notifyReferenceUrl;
    logger.info("notify ref change server, change type: {}, {}, uri: {}, servers: {}", changeType, reference, uri, urls);
    doNotify(urls, uri, "reference", new Function<String, Request>() {
        @Override
        public Request apply(String url) {
            return getRequest(url, reference, changeType);
        }
    });
}
 
Example #8
Source File: NotifyServiceImpl.java    From qconfig with MIT License 6 votes vote down vote up
@Override
public void notifyPushIp(final ConfigMeta meta, final long version, List<Host> destinations) {
    List<String> serverUrls = getServerUrls();
    if (serverUrls.isEmpty()) {
        logger.warn("notify push server, {}, version: {}, but no server, {}", meta, version, destinations);
        return;
    }

    String uri = this.notifyIpPushUrl;
    logger.info("notify push server, {}, version: {}, uri: {}, servers: {}, {}", meta, version, uri, serverUrls, destinations);
    StringBuilder sb = new StringBuilder();
    for (Host item : destinations) {
        sb.append(item.getIp()).append(Constants.LINE);
    }
    final String destinationsStr = sb.toString();

    doNotify(serverUrls, uri, "admin/notifyIpPush", new Function<String, Request>() {
        @Override
        public Request apply(String url) {
            AsyncHttpClient.BoundRequestBuilder builder = getBoundRequestBuilder(url, meta, version, destinationsStr);
            return builder.build();
        }
    });
}
 
Example #9
Source File: NotifyServiceImpl.java    From qconfig with MIT License 6 votes vote down vote up
@Override
public void notifyPush(final ConfigMeta meta, final long version, List<PushItemWithHostName> destinations) {
    List<String> serverUrls = getServerUrls();
    if (serverUrls.isEmpty()) {
        logger.warn("notify push server, {}, version: {}, but no server, {}", meta, version, destinations);
        return;
    }

    String uri = this.notifyPushUrl;
    logger.info("notify push server, {}, version: {}, uri: {}, servers: {}, {}", meta, version, uri, serverUrls, destinations);
    StringBuilder sb = new StringBuilder();
    for (PushItemWithHostName item : destinations) {
        sb.append(item.getHostname()).append(',')
                .append(item.getIp()).append(',')
                .append(item.getPort()).append(Constants.LINE);
    }
    final String destinationsStr = sb.toString();
    doNotify(serverUrls, uri, "push", new Function<String, Request>() {
        @Override
        public Request apply(String url) {
            AsyncHttpClient.BoundRequestBuilder builder = getBoundRequestBuilder(url, meta, version, destinationsStr);
            return builder.build();
        }
    });
}
 
Example #10
Source File: NingCookieExtractorV1.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Override
public String getCookie(Request request) {
    final Collection<Cookie> cookies = request.getCookies();
    if (cookies.isEmpty()) {
        return null;
    }
    final StringBuilder sb = new StringBuilder();
    Iterator<Cookie> iterator = cookies.iterator();
    while (iterator.hasNext()) {
        final Cookie cookie = iterator.next();
        sb.append(cookie.getName());
        sb.append('=');
        sb.append(cookie.getValue());
        if (iterator.hasNext()) {
            sb.append(',');
        }
    }
    return sb.toString();
}
 
Example #11
Source File: LoggingAsyncHandlerWrapper.java    From parsec-libraries with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor.
 *
 * @param asyncHandler asyncHandler
 * @param loggingPredicate
 */
public LoggingAsyncHandlerWrapper(final AsyncHandler<T> asyncHandler, final Request ningRequest,
                                  BiPredicate<Request, ResponseOrThrowable> loggingPredicate,
                                  NingRequestResponseFormatter formatter,
                                  String loggerName) {

    this.asyncHandler = asyncHandler;
    extensions = (asyncHandler instanceof AsyncHandlerExtensions)
            ? (AsyncHandlerExtensions) asyncHandler : null;
    progressAsyncHandler = (asyncHandler instanceof ProgressAsyncHandler)
            ? (ProgressAsyncHandler<T>) asyncHandler : null;
    this.progress = new ParsecAsyncProgress();
    this.ningRequest = ningRequest;
    this.loggingPredicate = loggingPredicate;
    this.formatter = formatter;
    this.traceLogger = LoggerFactory.getLogger(loggerName);
}
 
Example #12
Source File: HttpRequestClient.java    From qconfig with MIT License 6 votes vote down vote up
public <R> JsonV2<R> get(String url, Map<String, String> params, TypeReference<JsonV2<R>> typeReference) {
    AsyncHttpClient.BoundRequestBuilder prepareGet = httpClient.prepareGet(url);
    prepareGet.setHeader("Accept", "application/json");
    prepareGet.setHeader("Content-Type", "application/json; charset=utf-8");
    if (!CollectionUtils.isEmpty(params)) {
        for (Map.Entry<String, String> param : params.entrySet()) {
            prepareGet.addQueryParam(param.getKey(), param.getValue());
        }
    }
    Request request = prepareGet.build();
    try {
        Response response = httpClient.executeRequest(request).get(TIMEOUT_MS, TimeUnit.MILLISECONDS);
        int statusCode = response.getStatusCode();
        if (statusCode != Constants.OK_STATUS) {
            throw new RuntimeException(String.format("http request error,url:%s,status code:%d",url,statusCode));
        }
        return mapper.readValue(response.getResponseBody(DEFAULT_CHARSET), typeReference);
    }  catch (Exception e) {
        logger.error("request failOf, url [{}], params {}", url, params, e);
        throw new RuntimeException(e);
    }
}
 
Example #13
Source File: NingAsyncHttpRequest.java    From junit-servers with MIT License 6 votes vote down vote up
@Override
protected HttpResponse doExecute() throws Exception {
	final HttpUrl endpoint = getEndpoint();
	final String scheme = endpoint.getScheme();
	final String userInfo = null;
	final String host = endpoint.getHost();
	final int port = endpoint.getPort();
	final String path = UTF8UrlEncoder.encodePath(endpoint.getPath());
	final String query = null;
	final Uri uri = new Uri(scheme, userInfo, host, port, path, query);

	final String method = getMethod().getVerb();
	final RequestBuilder builder = new RequestBuilder(method, true).setUri(uri);

	handleQueryParameters(builder);
	handleBody(builder);
	handleHeaders(builder);
	handleCookies(builder);

	final Request request = builder.build();
	final long start = nanoTime();
	final Response response = client.executeRequest(request).get();
	final long duration = nanoTime() - start;

	return NingAsyncHttpResponseFactory.of(response, duration);
}
 
Example #14
Source File: ProfilingFormatter.java    From parsec-libraries with Apache License 2.0 6 votes vote down vote up
@Override
public String format(Request req, Response resp, Map<String, Object> additionalArgs) {
    int requestCount = NumberUtils.toInt(req.getHeaders()
            .getFirstValue(ParsecClientDefine.HEADER_PROFILING_REQUEST_COUNT));

    int lastRespCode = NumberUtils.toInt(req.getHeaders()
            .getFirstValue(ParsecClientDefine.HEADER_PROFILING_LAST_RESPONSE_CODE), 0);

    ParsecAsyncProgress progress = (ParsecAsyncProgress)additionalArgs.get(ParsecClientDefine.PROFILING_ASYNC_PROGRESS);

    String requestStatus = ParsecClientDefine.REQUEST_SINGLE;
    if (requestCount > 1) {
        requestStatus = ParsecClientDefine.REQUEST_SINGLE_RETRY + ":" + lastRespCode;
    }
    return ParsecClientProfilingLogUtil.formatMessage(req, resp, requestStatus, progress, null);
}
 
Example #15
Source File: ExecuteRequestInterceptor.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
public ExecuteRequestInterceptor(TraceContext traceContext, MethodDescriptor descriptor) {
    this.traceContext = traceContext;
    this.descriptor = descriptor;
    final NingAsyncHttpClientPluginConfig config = new NingAsyncHttpClientPluginConfig(traceContext.getProfilerConfig());

    ClientRequestAdaptor<Request> clientRequestAdaptor = new NingAsyncHttpClientRequestAdaptorV1();
    this.clientRequestRecorder = new ClientRequestRecorder<Request>(config.isParam(), clientRequestAdaptor);

    CookieExtractor<Request> cookieExtractor = NingCookieExtractorV1.INSTANCE;
    this.cookieRecorder = CookieRecorderFactory.newCookieRecorder(config.getHttpDumpConfig(), cookieExtractor);

    EntityExtractor<Request> entityExtractor = NingEntityExtractorV1.INSTANCE;
    this.entityRecorder = EntityRecorderFactory.newEntityRecorder(config.getHttpDumpConfig(), entityExtractor);

    ClientHeaderAdaptor<Request> clientHeaderAdaptor = new RequestHeaderAdaptorV1();
    this.requestTraceWriter = new DefaultRequestTraceWriter<Request>(clientHeaderAdaptor, traceContext);
}
 
Example #16
Source File: AsyncUtil.java    From httpsig-java with The Unlicense 6 votes vote down vote up
/**
 * Executes and replays a login request until one is found which satisfies the
 * {@link net.adamcin.httpsig.api.Challenge} being returned by the server, or until there are no more keys in the
 * keychain.
 *
 * @since 1.0.4
 *
 * @param client the {@link AsyncHttpClient} to which the {@link Signer} will be attached
 * @param signer the {@link Signer} used for login and subsequent signature authentication
 * @param loginRequest the login {@link Request} to be executed and replayed while rotating the keychain
 * @param responseHandler an {@link AsyncCompletionHandler} of type {@code T}
 * @param calcBefore provide another {@link SignatureCalculator} to call (such as a Content-MD5 generator) prior to
 *                   generating the signature for authentication.
 * @param <T> type parameter for completion handler
 * @return a {@link Future} of type {@code T}
 * @throws IOException if thrown by a login request
 */
public static <T> Future<T> login(final AsyncHttpClient client,
                         final Signer signer,
                         final Request loginRequest,
                         final AsyncCompletionHandler<T> responseHandler,
                         final SignatureCalculator calcBefore) throws IOException {

    final AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder(client.getConfig());
    configBuilder.addResponseFilter(new RotateAndReplayResponseFilter(signer));
    AsyncHttpClient loginClient = new AsyncHttpClient(configBuilder.build());

    enableAuth(loginClient, signer, calcBefore);
    Future<T> response = loginClient.executeRequest(loginClient
                                                            .prepareRequest(loginRequest)
                                                            .setUrl(loginRequest.getUrl()).build(),
                                                    responseHandler);
    enableAuth(client, signer, calcBefore);
    return response;
}
 
Example #17
Source File: ParsecClientProfilingLogUtilTest.java    From parsec-libraries with Apache License 2.0 6 votes vote down vote up
@BeforeTest
public void setupCommon(){
    req = mock(Request.class);
    when(req.getUri()).thenReturn(Uri.create("http://test.yahoo.com"));
    when(req.getMethod()).thenReturn("POST");
    when(req.getHeaders()).thenReturn(new FluentCaseInsensitiveStringsMap().add(ParsecClientDefine.HEADER_HOST,
            Arrays.asList("dummyhost")));

    resp = mock(Response.class);
    when(resp.getHeader(anyString())).thenReturn("mock resp header");
    when(resp.getHeader("content-length")).thenReturn("100");
    when(resp.getStatusCode()).thenReturn(200);



    reqStatus = "test_status";
    progress = new ParsecAsyncProgress();

}
 
Example #18
Source File: AsyncHttpClientHelperTest.java    From riposte with Apache License 2.0 5 votes vote down vote up
private void verifyRequestBuilderWrapperGeneratedAsExpected(
    RequestBuilderWrapper rbw, String url, String method, Optional<CircuitBreaker<Response>> customCb,
    boolean disableCb
) {
    assertThat(rbw.url).isEqualTo(url);
    assertThat(rbw.httpMethod).isEqualTo(method);
    assertThat(rbw.customCircuitBreaker).isEqualTo(customCb);
    assertThat(rbw.disableCircuitBreaker).isEqualTo(disableCb);
    Request req = rbw.requestBuilder.build();
    assertThat(req.getMethod()).isEqualTo(method);
    assertThat(req.getUri()).isEqualTo(Uri.create(url));
    assertThat(req.getUrl()).isEqualTo(url);
    assertThat(req.getNameResolver()).isEqualTo(MultiIpAwareNameResolver.INSTANCE);
}
 
Example #19
Source File: ParsecClientProfilingLogUtil.java    From parsec-libraries with Apache License 2.0 5 votes vote down vote up
/**
 *log remote profiling log.
 *
 * @param request ning http request
 * @param response ning http response
 * @param requestStatus request status
 * @param progress parsec async progress do
 */
public static void logRemoteRequest(
        final Request request,
        final Response response,
        final String requestStatus,
        final ParsecAsyncProgress progress
    ){
    logRemoteRequest(request, response, requestStatus, progress, null);
}
 
Example #20
Source File: NingJsonFormatter.java    From parsec-libraries with Apache License 2.0 5 votes vote down vote up
@Override
public String format(Request req, Response resp, Map<String, Object> additionalArgs) {
    ObjectNode root = _OBJECT_MAPPER.createObjectNode();
    root.put("time", (System.currentTimeMillis() / 1000L));
    fillNode(req, root.with("request"));
    fillNode(resp, root.with("response"));

    additionalArgs.forEach((key, value) -> root.putPOJO(key, value));

    try {
        return _OBJECT_MAPPER.writeValueAsString(root);
    } catch (JsonProcessingException e) {
        throw new RuntimeException(e);
    }
}
 
Example #21
Source File: LoggingAsyncHandlerWrapper.java    From parsec-libraries with Apache License 2.0 5 votes vote down vote up
private void logResponseOrThrowable(Request ningRequest, Response ningResponse, Throwable throwable, ParsecAsyncProgress progress) {

        if (!loggingPredicate.test(ningRequest, new ResponseOrThrowable(ningResponse, throwable))) {
            return;
        }

        Map<String, Object> additionalArgs = createAdditionalArgs(progress,  throwable);
        String messageToLog = formatter.format(ningRequest, ningResponse, additionalArgs);
        if(traceLogger.isTraceEnabled()) {
            traceLogger.trace(messageToLog);
        }
    }
 
Example #22
Source File: ParsecHttpRequestRetryCallable.java    From parsec-libraries with Apache License 2.0 5 votes vote down vote up
/**
 * Execute Request.
 *
 * @param ningRequest Ning request
 * @return T
 * @throws InterruptedException Interrupted exception
 * @throws ExecutionException   Execution exception
 */
@SuppressWarnings("unchecked")
private T executeRequest(Request ningRequest) throws InterruptedException, ExecutionException {
    if (asyncHandler != null) {
        return client.executeRequest(ningRequest, asyncHandler).get();
    } else {
        return (T) client.executeRequest(ningRequest).get();
    }
}
 
Example #23
Source File: AsyncUtil.java    From httpsig-java with The Unlicense 5 votes vote down vote up
/**
 * Executes and replays a login request until one is found which satisfies the
 * {@link net.adamcin.httpsig.api.Challenge} being returned by the server, or until there are no more keys in the
 * keychain.
 * @param client the {@link AsyncHttpClient} to which the {@link Signer} will be attached
 * @param signer the {@link Signer} used for login and subsequent signature authentication
 * @param loginRequest the login {@link Request} to be executed and replayed while rotating the keychain
 * @return a {@link Future} expecting a {@link Response}
 * @throws IOException if a request throws an exception
 */
public static Future<Response> login(final AsyncHttpClient client,
                                     final Signer signer,
                                     final Request loginRequest)
        throws IOException {

    return login(client, signer, loginRequest, new AsyncCompletionHandler<Response>() {
        @Override
        public Response onCompleted(Response response) throws Exception {
            return response;
        }
    });
}
 
Example #24
Source File: Zendesk.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
private Request req(String method, Uri template) {
    RequestBuilder builder = new RequestBuilder(method);
    if (realm != null) {
        builder.setRealm(realm);
    } else {
        builder.addHeader("Authorization", "Bearer " + oauthToken);
    }
    builder.setUrl(template.toString());
    return builder.build();
}
 
Example #25
Source File: RequestResponeLoggingFilter.java    From parsec-libraries with Apache License 2.0 5 votes vote down vote up
public RequestResponeLoggingFilter(BiPredicate<Request, ResponseOrThrowable> logPredicate,
                                   NingRequestResponseFormatter formatter,
                                   String loggerName) {
    this.predicate = logPredicate;
    this.formatter = formatter;
    this.traceLoggerName = loggerName;
}
 
Example #26
Source File: AsyncHttpConnection.java    From tez with Apache License 2.0 5 votes vote down vote up
/**
 * Connect to source
 *
 * @return true if connection was successful
 * false if connection was previously cleaned up
 * @throws IOException upon connection failure
 */
public boolean connect() throws IOException, InterruptedException {
  computeEncHash();

  RequestBuilder rb = new RequestBuilder();
  rb.setHeader(SecureShuffleUtils.HTTP_HEADER_URL_HASH, encHash);
  rb.setHeader(ShuffleHeader.HTTP_HEADER_NAME, ShuffleHeader.DEFAULT_HTTP_HEADER_NAME);
  rb.setHeader(ShuffleHeader.HTTP_HEADER_VERSION, ShuffleHeader.DEFAULT_HTTP_HEADER_VERSION);
  Request request = rb.setUrl(url.toString()).build();

  //for debugging
  LOG.debug("Request url={}, encHash={}, id={}", url, encHash);

  try {
    //Blocks calling thread until it receives headers, but have the option to defer response body
    responseFuture = httpAsyncClient.executeRequest(request, handler);

    //BodyDeferringAsyncHandler would automatically manage producer and consumer frequency mismatch
    dis = new TezBodyDeferringAsyncHandler.BodyDeferringInputStream(responseFuture, handler, pis);

    response = dis.getAsapResponse();
    if (response == null) {
      throw new IOException("Response is null");
    }
  } catch(IOException e) {
    throw e;
  }

  //verify the response
  int rc = response.getStatusCode();
  if (rc != HttpURLConnection.HTTP_OK) {
    LOG.debug("Request url={}, id={}", response.getUri());
    throw new IOException("Got invalid response code " + rc + " from "
        + url + ": " + response.getStatusText());
  }
  return true;
}
 
Example #27
Source File: ExecuteRequestInterceptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Override
public void before(Object target, Object[] args) {
    if (isDebug) {
        logger.beforeInterceptor(target, args);
    }

    final Trace trace = traceContext.currentRawTraceObject();
    if (trace == null) {
        return;
    }

    if (!validate(args)) {
        return;
    }

    final Request httpRequest = (Request) args[0];
    final boolean sampling = trace.canSampled();
    if (!sampling) {
        if (httpRequest != null) {
            this.requestTraceWriter.write(httpRequest);
        }
        return;
    }

    trace.traceBlockBegin();
    final SpanEventRecorder recorder = trace.currentSpanEventRecorder();
    final TraceId nextId = trace.getTraceId().getNextTraceId();
    recorder.recordNextSpanId(nextId.getSpanId());
    recorder.recordServiceType(NingAsyncHttpClientConstants.ASYNC_HTTP_CLIENT);

    if (httpRequest != null) {
        String host = getHost(httpRequest);
        requestTraceWriter.write(httpRequest, nextId, host);
    }
}
 
Example #28
Source File: Zendesk.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
private Request req(String method, Uri template, int page) {
    RequestBuilder builder = new RequestBuilder(method);
    if (realm != null) {
        builder.setRealm(realm);
    } else {
        builder.addHeader("Authorization", "Bearer " + oauthToken);
    }
    builder.addQueryParameter("page", Integer.toString(page));
    builder.setUrl(template.toString().replace("%2B", "+")); //replace out %2B with + due to API restriction
    return builder.build();
}
 
Example #29
Source File: Zendesk.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
private Request req(String method, Uri template, String contentType, byte[] body) {
    RequestBuilder builder = new RequestBuilder(method);
    if (realm != null) {
        builder.setRealm(realm);
    } else {
        builder.addHeader("Authorization", "Bearer " + oauthToken);
    }
    builder.setUrl(template.toString());
    builder.addHeader("Content-type", contentType);
    builder.setBody(body);
    return builder.build();
}
 
Example #30
Source File: GithubRepositoryApiImpl.java    From bistoury with GNU General Public License v3.0 5 votes vote down vote up
private ApiResult doFile(final String project, final String ref, final String path) {
    Optional<PrivateToken> privateToken = privateTokenService.queryToken(LoginContext.getLoginContext().getLoginUser());
    if (!privateToken.isPresent()) {
        return ResultHelper.fail(-1, "尚未设置 Github Private Token");
    }

    String fileUrl = buildFileUrl(project, path);
    Request request = client.prepareGet(fileUrl)
            .addQueryParam("ref", ref)
            .addHeader("Accept", "application/json")
            .addHeader("'content-type", "application/json")
            .addHeader("Authorization", "token " + privateToken.get().getPrivateToken())
            .build();
    try {
        Response response = client.executeRequest(request).get();
        int statusCode = response.getStatusCode();
        switch (statusCode) {
            case 200:
                String responseBody = response.getResponseBody(Charsets.UTF_8.name());
                return ResultHelper.success(JacksonSerializer.deSerialize(responseBody, GitHubFile.class));
            case 401:
                return ResultHelper.fail("拒绝访问,请检查private token");
            case 404:
                return ResultHelper.fail("文件不存在,请检查链接:" + fileUrl);
            default:
                return ResultHelper.fail("请求github失败,位置的状态码:" + statusCode);
        }
    } catch (Exception e) {
        Metrics.counter("connect_github_error").inc();
        logger.error("connect github fail", e);
        return ResultHelper.fail("连接 github 失败" + e.getMessage());
    }
}