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

io.joshworks.snappy.handler.HttpRootHandler Maven / Gradle / Ivy

There is a newer version: 0.6.0
Show newest version
/*
 * Copyright 2017 Josue Gontijo
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package io.joshworks.snappy.handler;

import io.undertow.server.HttpServerExchange;
import io.undertow.server.RoutingHandler;

/**
 * Created by Josh Gontijo on 3/17/17.
 */
public class HttpRootHandler extends RoutingHandler {

    public HttpRootHandler(boolean rewriteQueryParameters) {
        super(rewriteQueryParameters);
    }

    @Override
    public void handleRequest(HttpServerExchange exchange) throws Exception {
        removeTrailingSlash(exchange);
        super.handleRequest(exchange);
    }

    /**
     * 

* Handler that removes trailing slash from incoming requests in order to avoid null path parameters * For example: * For a given resource /users/{id} * /users/ would match with the resource, passing in an null id * Instead it would rewrite to * /users causing a 404, thus avoiding null path parameter */ private void removeTrailingSlash(HttpServerExchange exchange) { String trailingSlash = "/"; String requestPath = exchange.getRelativePath(); if (requestPath != null && !requestPath.isEmpty() && !requestPath.equals(trailingSlash)) { requestPath = requestPath.endsWith(trailingSlash) ? requestPath.substring(0, requestPath.length() - 1) : requestPath; exchange.setRelativePath(requestPath); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy