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

com.envimate.httpmate.jetty.JettyEndpointHandler Maven / Gradle / Ivy

There is a newer version: 1.0.26
Show newest version
/*
 * Copyright (c) 2018 envimate GmbH - https://envimate.com/.
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 com.envimate.httpmate.jetty;

import com.envimate.httpmate.HttpMate;
import com.envimate.httpmate.request.RawHttpRequest;
import com.envimate.httpmate.response.HttpResponse;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import static com.envimate.httpmate.request.RawHttpRequest.rawHttpRequest;
import static com.envimate.httpmate.util.Streams.streamInputStreamToOutputStream;
import static com.envimate.httpmate.util.Validators.validateNotNull;

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
final class JettyEndpointHandler extends AbstractHandler {
    private final HttpMate httpMate;

    static AbstractHandler jettyEndpointHandler(final HttpMate httpMate) {
        validateNotNull(httpMate, "httpMate");
        return new JettyEndpointHandler(httpMate);
    }

    @Override
    public void handle(final String target,
                       final Request request,
                       final HttpServletRequest httpServletRequest,
                       final HttpServletResponse httpServletResponse) throws IOException {
        final String method = request.getMethod();
        final String path = request.getPathInfo();
        final Map headers = extractHeaders(request);
        final Map queryParameters = extractQueryParameters(request);
        final InputStream body = request.getInputStream();
        final RawHttpRequest rawHttpRequest = rawHttpRequest(method, path, queryParameters, headers, body);
        final HttpResponse httpResponse = httpMate.handleRequest(rawHttpRequest);
        httpResponse.headers().forEach(httpServletResponse::setHeader);
        httpServletResponse.setStatus(httpResponse.status());
        final OutputStream outputStream = httpServletResponse.getOutputStream();
        streamInputStreamToOutputStream(httpResponse.body(), outputStream);
    }

    private static Map extractHeaders(final HttpServletRequest request) {
        final Enumeration headerNames = request.getHeaderNames();
        final Map headers = new HashMap<>();
        while (headerNames.hasMoreElements()) {
            final String headerName = headerNames.nextElement();
            final String value = request.getHeader(headerName);
            headers.put(headerName, value);
        }
        return headers;
    }

    private static Map extractQueryParameters(final HttpServletRequest request) {
        final Enumeration parameterNames = request.getParameterNames();
        final Map queryParameters = new HashMap<>();
        while (parameterNames.hasMoreElements()) {
            final String parameterName = parameterNames.nextElement();
            final String value = request.getParameter(parameterName);
            queryParameters.put(parameterName, value);
        }
        return queryParameters;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy