Java Code Examples for io.undertow.server.HttpServerExchange#getHostName()

The following examples show how to use io.undertow.server.HttpServerExchange#getHostName() . 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: SinglePortConfidentialityHandler.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
protected URI getRedirectURI(HttpServerExchange exchange, int port) throws URISyntaxException {
    String host = exchange.getHostName();

    String queryString = exchange.getQueryString();
    String uri = exchange.getRequestURI();
    if(exchange.isHostIncludedInRequestURI()) {
        int slashCount = 0;
        for(int i = 0; i < uri.length(); ++i) {
            if(uri.charAt(i) == '/') {
                slashCount++;
                if(slashCount == 3) {
                    uri = uri.substring(i);
                    break;
                }
            }
        }
    }
    return new URI("https", null, host, port, uri,
            queryString == null || queryString.length() == 0 ? null : queryString, null);
}
 
Example 2
Source File: NameVirtualHostDefaultHandler.java    From galeb with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void handleRequest(HttpServerExchange exchange) throws Exception {
    final String hostName = exchange.getHostName();
    final NameVirtualHostHandler nameVirtualHostHandler = context.getBean(NameVirtualHostHandler.class);
    if (existHostname(hostName)) {            
        if (!nameVirtualHostHandler.getHosts().containsKey(hostName)) { 
        	logger.info("adding " + hostName);
        	final VirtualHost virtualHost = cache.get(hostName);
        	nameVirtualHostHandler.addHost(hostName, defineNextHandler(virtualHost));
        }
        
        nameVirtualHostHandler.handleRequest(exchange);
    } else {
        ResponseCodeOnError.VIRTUALHOST_NOT_FOUND.getHandler().handleRequest(exchange);
    }
}
 
Example 3
Source File: SinglePortConfidentialityHandler.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
protected URI getRedirectURI(HttpServerExchange exchange, int port) throws URISyntaxException {
    String host = exchange.getHostName();

    String queryString = exchange.getQueryString();
    String uri = exchange.getRequestURI();
    if(exchange.isHostIncludedInRequestURI()) {
        int slashCount = 0;
        for(int i = 0; i < uri.length(); ++i) {
            if(uri.charAt(i) == '/') {
                slashCount++;
                if(slashCount == 3) {
                    uri = uri.substring(i);
                    break;
                }
            }
        }
    }
    return new URI("https", null, host, port, uri,
            queryString == null || queryString.length() == 0 ? null : queryString, null);
}
 
Example 4
Source File: StatsdCompletionListener.java    From galeb with Apache License 2.0 5 votes vote down vote up
@Override
public void exchangeEvent(HttpServerExchange exchange, NextListener nextListener) {
    try {
        String virtualhost = exchange.getHostName();
        virtualhost = virtualhost != null ? virtualhost : UNDEF;
        String poolName = exchange.getAttachment(POOL_NAME);
        poolName = poolName != null ? poolName : UNDEF;
        String targetUri = exchange.getAttachment(HostSelector.REAL_DEST);
        targetUri = targetUri != null ? targetUri : extractXGalebErrorHeader(exchange.getResponseHeaders());
        final boolean isTargetUnknown = targetIsUnknown(targetUri);
        final Integer statusCode = exchange.getStatusCode();
        final String method = exchange.getRequestMethod().toString();
        final Integer responseTime = getResponseTime(exchange);

        // @formatter:off
        final String key = ENV_TAG    + environmentName + "." +
                           VH_TAG     + cleanUpKey(virtualhost) + "." +
                           POOL_TAG   + cleanUpKey(poolName) + "." +
                           TARGET_TAG + cleanUpKey(targetUri);
        // @formatter:on

        sendStatusCodeCount(key, statusCode, isTargetUnknown);
        sendHttpMethodCount(key, method);
        sendResponseTime(key, responseTime, isTargetUnknown);
        if (sendOpenconnCounter) sendActiveConnCount(key, exchange.getAttachment(ClientStatisticsMarker.TARGET_CONN), isTargetUnknown);

    } catch (Exception e) {
        logger.error(ExceptionUtils.getStackTrace(e));
    } finally {
        nextListener.proceed();
    }
}
 
Example 5
Source File: LocalServerNameAttribute.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Override
public String readAttribute(final HttpServerExchange exchange) {
    return exchange.getHostName();
}
 
Example 6
Source File: LocalServerNameAttribute.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public String readAttribute(final HttpServerExchange exchange) {
    return exchange.getHostName();
}
 
Example 7
Source File: RegisterHandler.java    From PYX-Reloaded with Apache License 2.0 4 votes vote down vote up
@NotNull
@Override
public JsonWrapper handle(@Nullable User user, Parameters params, HttpServerExchange exchange) throws BaseJsonHandler.StatusException {
    if (banList.contains(exchange.getHostName()))
        throw new BaseCahHandler.CahException(Consts.ErrorCode.BANNED);

    PreparingShutdown.get().check();

    Consts.AuthType type;
    try {
        type = Consts.AuthType.parse(params.getStringNotNull(Consts.GeneralKeys.AUTH_TYPE));
    } catch (ParseException ex) {
        throw new BaseCahHandler.CahException(Consts.ErrorCode.BAD_REQUEST, ex);
    }

    UserAccount account;
    String nickname;
    switch (type) {
        case PASSWORD:
            nickname = params.getStringNotNull(Consts.UserData.NICKNAME);
            if (!Pattern.matches(Consts.VALID_NAME_PATTERN, nickname))
                throw new BaseCahHandler.CahException(Consts.ErrorCode.INVALID_NICK);

            account = accounts.getPasswordAccountForNickname(nickname);
            if (account == null) { // Without account
                user = new User(nickname, exchange.getHostName(), Sessions.generateNewId());
            } else {
                String password = params.getStringNotNull(Consts.AuthType.PASSWORD);
                if (password.isEmpty() || !BCrypt.checkpw(password, ((PasswordAccount) account).hashedPassword))
                    throw new BaseCahHandler.CahException(Consts.ErrorCode.WRONG_PASSWORD);

                user = User.withAccount(account, exchange.getHostName());
            }
            break;
        case GOOGLE:
            if (!socialLogin.googleEnabled())
                throw new BaseCahHandler.CahException(Consts.ErrorCode.UNSUPPORTED_AUTH_TYPE);

            GoogleIdToken.Payload googleToken = socialLogin.verifyGoogle(params.getStringNotNull(Consts.AuthType.GOOGLE));
            if (googleToken == null) throw new BaseCahHandler.CahException(Consts.ErrorCode.GOOGLE_INVALID_TOKEN);

            account = accounts.getGoogleAccount(googleToken);
            if (account == null) throw new BaseCahHandler.CahException(Consts.ErrorCode.GOOGLE_NOT_REGISTERED);

            nickname = account.username;
            user = User.withAccount(account, exchange.getHostName());
            break;
        case FACEBOOK:
            if (!socialLogin.facebookEnabled())
                throw new BaseCahHandler.CahException(Consts.ErrorCode.UNSUPPORTED_AUTH_TYPE);

            FacebookToken facebookToken = socialLogin.verifyFacebook(params.getStringNotNull(Consts.AuthType.FACEBOOK));
            if (facebookToken == null)
                throw new BaseCahHandler.CahException(Consts.ErrorCode.FACEBOOK_INVALID_TOKEN);

            account = accounts.getFacebookAccount(facebookToken);
            if (account == null) throw new BaseCahHandler.CahException(Consts.ErrorCode.FACEBOOK_NOT_REGISTERED);

            nickname = account.username;
            user = User.withAccount(account, exchange.getHostName());
            break;
        case GITHUB:
            if (!socialLogin.githubEnabled())
                throw new BaseCahHandler.CahException(Consts.ErrorCode.UNSUPPORTED_AUTH_TYPE);

            String githubToken = params.getStringNotNull(Consts.AuthType.GITHUB);

            GithubProfileInfo githubInfo = socialLogin.infoGithub(githubToken);
            account = accounts.getGithubAccount(githubInfo);
            if (account == null) throw new BaseCahHandler.CahException(Consts.ErrorCode.GITHUB_NOT_REGISTERED);

            nickname = account.username;
            user = User.withAccount(account, exchange.getHostName());
            break;
        case TWITTER:
            if (!socialLogin.twitterEnabled())
                throw new BaseCahHandler.CahException(Consts.ErrorCode.UNSUPPORTED_AUTH_TYPE);

            String twitterTokens = params.getStringNotNull(Consts.AuthType.TWITTER);

            TwitterProfileInfo twitterInfo = socialLogin.infoTwitter(twitterTokens);
            account = accounts.getTwitterAccount(twitterInfo);
            if (account == null) throw new BaseCahHandler.CahException(Consts.ErrorCode.TWITTER_NOT_REGISTERED);

            nickname = account.username;
            user = User.withAccount(account, exchange.getHostName());
            break;
        default:
            throw new BaseCahHandler.CahException(Consts.ErrorCode.BAD_REQUEST);
    }

    User registeredUser = users.checkAndAdd(user);
    if (registeredUser != null) user = registeredUser;
    exchange.setResponseCookie(new CookieImpl("PYX-Session", Sessions.get().add(user)));

    return new JsonWrapper()
            .add(Consts.UserData.NICKNAME, nickname)
            .add(Consts.UserData.IS_ADMIN, user.isAdmin());
}