Search in sources :

Example 36 with Args

use of org.apache.hc.core5.util.Args in project mercury by yellow013.

the class ClientPreemptiveDigestAuthentication method main.

public static void main(final String[] args) throws Exception {
    try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {
        final HttpHost target = new HttpHost("http", "httpbin.org", 80);
        final HttpClientContext localContext = HttpClientContext.create();
        final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(new AuthScope(target), new UsernamePasswordCredentials("user", "passwd".toCharArray()));
        localContext.setCredentialsProvider(credentialsProvider);
        final HttpGet httpget = new HttpGet("http://httpbin.org/digest-auth/auth/user/passwd");
        System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri());
        for (int i = 0; i < 3; i++) {
            try (final CloseableHttpResponse response = httpclient.execute(target, httpget, localContext)) {
                System.out.println("----------------------------------------");
                System.out.println(response.getCode() + " " + response.getReasonPhrase());
                EntityUtils.consume(response.getEntity());
                final AuthExchange authExchange = localContext.getAuthExchange(target);
                if (authExchange != null) {
                    final AuthScheme authScheme = authExchange.getAuthScheme();
                    if (authScheme instanceof DigestScheme) {
                        final DigestScheme digestScheme = (DigestScheme) authScheme;
                        System.out.println("Nonce: " + digestScheme.getNonce() + "; count: " + digestScheme.getNounceCount());
                    }
                }
            }
        }
    }
}
Also used : DigestScheme(org.apache.hc.client5.http.impl.auth.DigestScheme) CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) BasicCredentialsProvider(org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider) AuthExchange(org.apache.hc.client5.http.auth.AuthExchange) HttpHost(org.apache.hc.core5.http.HttpHost) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) AuthScope(org.apache.hc.client5.http.auth.AuthScope) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials) AuthScheme(org.apache.hc.client5.http.auth.AuthScheme)

Example 37 with Args

use of org.apache.hc.core5.util.Args in project mercury by yellow013.

the class ProxyTunnelDemo method main.

public static final void main(final String[] args) throws Exception {
    final ProxyClient proxyClient = new ProxyClient();
    final HttpHost target = new HttpHost("www.yahoo.com", 80);
    final HttpHost proxy = new HttpHost("localhost", 8888);
    final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user", "pwd".toCharArray());
    try (final Socket socket = proxyClient.tunnel(proxy, target, credentials)) {
        final Writer out = new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.ISO_8859_1);
        out.write("GET / HTTP/1.1\r\n");
        out.write("Host: " + target.toHostString() + "\r\n");
        out.write("Agent: whatever\r\n");
        out.write("Connection: close\r\n");
        out.write("\r\n");
        out.flush();
        final BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.ISO_8859_1));
        String line = null;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ProxyClient(org.apache.hc.client5.http.impl.classic.ProxyClient) HttpHost(org.apache.hc.core5.http.HttpHost) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) Socket(java.net.Socket) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Example 38 with Args

use of org.apache.hc.core5.util.Args in project skywalking-java by apache.

the class HttpClientDoExecuteInterceptor method beforeMethod.

@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
    if (allArguments[0] == null || allArguments[1] == null) {
        // illegal args, can't trace. ignore.
        return;
    }
    final HttpHost httpHost = (HttpHost) allArguments[0];
    ClassicHttpRequest httpRequest = (ClassicHttpRequest) allArguments[1];
    final ContextCarrier contextCarrier = new ContextCarrier();
    String remotePeer = httpHost.getHostName() + ":" + port(httpHost);
    String uri = httpRequest.getUri().toString();
    String requestURI = getRequestURI(uri);
    String operationName = requestURI;
    AbstractSpan span = ContextManager.createExitSpan(operationName, contextCarrier, remotePeer);
    span.setComponent(ComponentsDefine.HTTPCLIENT);
    Tags.URL.set(span, buildURL(httpHost, uri));
    Tags.HTTP.METHOD.set(span, httpRequest.getMethod());
    SpanLayer.asHttp(span);
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        httpRequest.setHeader(next.getHeadKey(), next.getHeadValue());
    }
}
Also used : ContextCarrier(org.apache.skywalking.apm.agent.core.context.ContextCarrier) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) CarrierItem(org.apache.skywalking.apm.agent.core.context.CarrierItem) HttpHost(org.apache.hc.core5.http.HttpHost) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Example 39 with Args

use of org.apache.hc.core5.util.Args in project log-helper by Redick01.

the class HttpClient5Example method main.

public static void main(String[] args) {
    String url = "http://127.0.0.1:8081/order/getPayCount?orderNo=1";
    CloseableHttpClient client = HttpClientBuilder.create().addRequestInterceptorFirst(new TraceIdIdHttpClient5Interceptor()).build();
    HttpGet get = new HttpGet(url);
    try {
        CloseableHttpResponse response = client.execute(get);
        HttpEntity entity = response.getEntity();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) HttpEntity(org.apache.hc.core5.http.HttpEntity) TraceIdIdHttpClient5Interceptor(com.redick.support.httpclient.TraceIdIdHttpClient5Interceptor) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) IOException(java.io.IOException)

Example 40 with Args

use of org.apache.hc.core5.util.Args in project lobster by lundylizard.

the class PlayCommand method action.

@Override
public void action(String[] args, @NotNull MessageReceivedEvent event) {
    var link = new StringBuilder();
    var channel = event.getTextChannel();
    var self = Objects.requireNonNull(event.getMember()).getGuild().getSelfMember();
    var selfVoiceState = self.getVoiceState();
    var member = event.getMember();
    var memberVoiceState = member.getVoiceState();
    var spotify = new SpotifyToYoutubeInterpreter();
    var top = false;
    assert memberVoiceState != null;
    if (!memberVoiceState.inVoiceChannel()) {
        channel.sendMessage(":warning: You are not in a voice channel.").queue();
        return;
    }
    assert selfVoiceState != null;
    if (!selfVoiceState.inVoiceChannel()) {
        var audioManager = event.getGuild().getAudioManager();
        var memberChannel = Objects.requireNonNull(event.getMember().getVoiceState()).getChannel();
        audioManager.openAudioConnection(memberChannel);
        assert memberChannel != null;
        event.getChannel().sendMessage(":loud_sound: Connecting to voice channel `\uD83D\uDD0A " + memberChannel.getName() + "`").queue();
    }
    if (args[0].equalsIgnoreCase("top")) {
        top = true;
    }
    for (var i = top ? 1 : 0; i < args.length; i++) {
        link.append(args[i]).append(" ");
    }
    if (!isUrl(link.toString())) {
        link.insert(0, "ytsearch:");
    }
    if (!event.getMessage().getAttachments().isEmpty()) {
        link.delete(0, link.length());
        link.append(event.getMessage().getAttachments().get(0).getUrl());
    }
    if (link.isEmpty()) {
        channel.sendMessage(":warning: Please provide a file or an URL.").queue();
        return;
    }
    // Spotify doesn't allow direct playback from their API, so it's getting the data from the song and searches it on YouTube
    if (spotify.isSpotifyLink(link.toString())) {
        if (Lobsterbot.DEBUG)
            System.out.println(spotify.isSpotifyPlaylist(link.toString()));
        if (Lobsterbot.DEBUG)
            System.out.println(link.toString().replace("\\?si=.*$", "").replace("https://open.spotify.com/", ""));
        if (!spotify.isSpotifyPlaylist(link.toString())) {
            var spotifyId = spotify.getSpotifyIdFromLink(link.toString());
            link.delete(0, link.length());
            try {
                link.append("ytsearch:").append(spotify.getArtistFromSpotify(spotifyId)).append(" ").append(spotify.getSongNameFromSpotify(spotifyId));
            } catch (IOException | ParseException | SpotifyWebApiException e) {
                e.printStackTrace();
            }
        } else {
            channel.sendMessage("Spotify Playlists may be a little buggy, gonna fix that sooner or later.").queue();
            var spotifyId = spotify.getSpotifyIdFromLink(link.toString());
            Playlist playlist = null;
            try {
                playlist = spotify.getSpotifyPlaylist(spotifyId);
            } catch (IOException | ParseException | SpotifyWebApiException e) {
                e.printStackTrace();
            }
            assert playlist != null;
            for (PlaylistTrack playlistTrack : playlist.getTracks().getItems()) {
                try {
                    if (playlistTrack.getTrack().getId() != null) {
                        PlayerManager.getInstance().loadAndPlay(event, ("ytsearch:" + spotify.getArtistFromSpotify(playlistTrack.getTrack().getId()) + " " + spotify.getSongNameFromSpotify(playlistTrack.getTrack().getId())).trim(), top, true);
                    }
                } catch (IOException | SpotifyWebApiException | ParseException e) {
                    e.printStackTrace();
                }
            }
            event.getChannel().sendMessage(":arrow_forward: Added " + playlist.getTracks().getTotal() + " songs to the queue.").queue();
        }
    }
    PlayerManager.getInstance().loadAndPlay(event, link.toString().trim(), top, false);
}
Also used : Playlist(se.michaelthelin.spotify.model_objects.specification.Playlist) SpotifyToYoutubeInterpreter(de.lundy.lobster.lavaplayer.spotify.SpotifyToYoutubeInterpreter) PlaylistTrack(se.michaelthelin.spotify.model_objects.specification.PlaylistTrack) IOException(java.io.IOException) ParseException(org.apache.hc.core5.http.ParseException) SpotifyWebApiException(se.michaelthelin.spotify.exceptions.SpotifyWebApiException)

Aggregations

HttpHost (org.apache.hc.core5.http.HttpHost)32 HttpRequest (org.apache.hc.core5.http.HttpRequest)25 HttpResponse (org.apache.hc.core5.http.HttpResponse)24 StatusLine (org.apache.hc.core5.http.message.StatusLine)22 IOReactorConfig (org.apache.hc.core5.reactor.IOReactorConfig)22 IOException (java.io.IOException)21 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)19 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)19 HttpConnection (org.apache.hc.core5.http.HttpConnection)19 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)17 Header (org.apache.hc.core5.http.Header)17 HttpException (org.apache.hc.core5.http.HttpException)16 CountDownLatch (java.util.concurrent.CountDownLatch)13 SimpleHttpRequest (org.apache.hc.client5.http.async.methods.SimpleHttpRequest)13 SimpleHttpResponse (org.apache.hc.client5.http.async.methods.SimpleHttpResponse)12 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)12 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)12 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)12 EntityDetails (org.apache.hc.core5.http.EntityDetails)11 Http1StreamListener (org.apache.hc.core5.http.impl.Http1StreamListener)11