All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.hyperfoil.http.UserAgentAppender Maven / Gradle / Ivy

There is a newer version: 0.27.1
Show newest version
package io.hyperfoil.http;

import java.net.InetAddress;
import java.net.UnknownHostException;

import io.hyperfoil.http.api.HttpRequestWriter;
import io.hyperfoil.api.session.ResourceUtilizer;
import io.hyperfoil.api.session.Session;
import io.hyperfoil.function.SerializableBiConsumer;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.util.AsciiString;

public class UserAgentAppender implements SerializableBiConsumer, ResourceUtilizer, Session.ResourceKey {
   private static final String HOSTNAME;

   static {
      String hostname;
      try {
         hostname = InetAddress.getLocalHost().getHostName();
      } catch (UnknownHostException e) {
         hostname = "unknown";
      }
      HOSTNAME = hostname;
   }

   @Override
   public void accept(Session session, HttpRequestWriter httpRequestWriter) {
      httpRequestWriter.putHeader(HttpHeaderNames.USER_AGENT, session.getResource(this).id);
   }

   @Override
   public void reserve(Session session) {
      SessionId sessionId = new SessionId(new AsciiString("#" + session.uniqueId() + "@" + HOSTNAME));
      session.declareResource(this, () -> sessionId);
   }

   public static final class SessionId implements Session.Resource {
      public final AsciiString id;

      public SessionId(AsciiString id) {
         this.id = id;
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy