/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * 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.gravitee.gateway.standalone.vertx.ws;

import io.gravitee.common.http.HttpHeaders;
import io.gravitee.common.http.IdGenerator;
import io.gravitee.gateway.api.Request;
import io.gravitee.gateway.api.Response;
import io.gravitee.gateway.reactor.Reactor;
import io.gravitee.gateway.standalone.vertx.VertxReactorHandler;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerRequest;

/**
 * @author David BRASSELY (david.brassely at graviteesource.com)
 * @author Azize ELAMRANI (azize.elamrani at graviteesource.com)
 * @author GraviteeSource Team
 */
public class VertxWebSocketReactorHandler extends VertxReactorHandler {

    private final IdGenerator idGenerator;

    public VertxWebSocketReactorHandler(final Reactor reactor, IdGenerator idGenerator) {
        super(reactor, idGenerator);
        this.idGenerator = idGenerator;
    }

    @Override
    public void handle(HttpServerRequest httpServerRequest) {
        Request request;
        Response response;

        if (isWebSocket(httpServerRequest)) {
            request = new VertxWebSocketServerRequest(httpServerRequest, idGenerator);
            response = new VertxWebSocketServerResponse(httpServerRequest, request);
            route(request, response);
        } else {
            super.handle(httpServerRequest);
        }
    }

    private boolean isWebSocket(HttpServerRequest httpServerRequest) {
        String connectionHeader = httpServerRequest.getHeader(HttpHeaders.CONNECTION);
        String upgradeHeader = httpServerRequest.getHeader(HttpHeaders.UPGRADE);

        return httpServerRequest.method() == HttpMethod.GET &&
                HttpHeaderValues.UPGRADE.contentEqualsIgnoreCase(connectionHeader) &&
                HttpHeaderValues.WEBSOCKET.contentEqualsIgnoreCase(upgradeHeader);
    }
}

@@ -2252,25 +2252,24 @@
 GRADE);%0A
-%0A
         
 return h
@@ -2264,62 +2264,219 @@
     
-return httpServerRequest.method() == HttpMethod.GET &&
+boolean isUpgrade = false;%0A        if (connectionHeader != null) %7B%0A            String%5B%5D connectionParts = connectionHeader.split(%22,%22);%0A            for (int i = 0; i %3C connectionParts.length && !isUpgrade; ++i) %7B
 %0A   
@@ -2479,32 +2479,44 @@
 %0A               
+ isUpgrade =
  HttpHeaderValue
@@ -2559,23 +2559,146 @@
 nnection
-He
+Parts%5Bi%5D.trim());%0A            %7D%0A        %7D%0A        return httpServerRequest.method() == HttpMethod.GET &&%0A                isUpgr
 ade
-r)
  &&%0A    

test existance of Upgrade into Connection header values