/*
 * Copyright 2017 the original author or authors.
 *
 * 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 com.visionarts.powerjambda.models;

import java.util.Map;

import com.visionarts.powerjambda.AwsProxyRequest;
import com.visionarts.powerjambda.http.HttpMethod;

/**
 * The request to populate an action.
 *
 * @param <T> The type of body in an action request
 *
 */
public class ActionRequest<T> {

    private AwsProxyRequest request;
    private T body;

    public ActionRequest() {
        this(null);
    }

    public ActionRequest(AwsProxyRequest request) {
        this.request = request;
    }

    public AwsProxyRequest getOriginalRequest() {
        return request;
    }

    /**
     * Returns HTTP method.
     *
     * @return HTTP method
     */
    public HttpMethod getMethod() {
        return HttpMethod.valueOf(request.getHttpMethod());
    }

    /**
     * Returns the name of deployment stage on API Gateway.
     *
     * @return The name of stage
     */
    public String getStage() {
        return request.getRequestContext().getStage();
    }

    /**
     * Returns the stage variables defined for the stage in API Gateway.
     *
     * @return The stage variables
     */
    public Map<String, String> getStageVariables() {
        return request.getStageVariables();
    }

    /**
     * Returns the stage variable defined for the stage in API Gateway.
     *
     * @param key The key
     * @return The stage variable
     */
    public String getStageVariable(String key) {
        return request.getStageVariables().get(key);
    }

    public String getPath() {
        return request.getPath();
    }

    public Map<String, String> getHeaders() {
        return request.getHeaders();
    }

    public String getHeader(String key) {
        return request.getHeaders().get(key);
    }

    public Map<String, String> getPathParameters() {
        return request.getPathParameters();
    }

    public String getPathParameter(String key) {
        return request.getPathParameters().get(key);
    }

    public Map<String, String> getQueryParameters() {
        return request.getQueryStringParameters();
    }

    public String getQueryParameter(String key) {
        return request.getQueryStringParameters().get(key);
    }

    public T getBody() {
        return body;
    }

    public void setBody(T body) {
        this.body = body;
    }

    public String getBodyAsString() {
        return request.getBody();
    }
}

@@ -669,19 +669,76 @@
 va.util.
-Map
+Collections;%0Aimport java.util.Map;%0Aimport java.util.Optional
 ;%0A%0Aimpor
@@ -2463,32 +2463,52 @@
 %0A        return 
+Optional.ofNullable(
 request.getPathP
@@ -2510,32 +2510,81 @@
 PathParameters()
+)%0A                .orElse(Collections.emptyMap())
 ;%0A    %7D%0A%0A    pub
@@ -2636,32 +2636,24 @@
      return 
-request.
 getPathParam
@@ -2738,32 +2738,52 @@
 %0A        return 
+Optional.ofNullable(
 request.getQuery
@@ -2796,24 +2796,73 @@
 Parameters()
+)%0A                .orElse(Collections.emptyMap())
 ;%0A    %7D%0A%0A   
@@ -2923,32 +2923,24 @@
  return 
-request.
 getQuery
 StringPa
@@ -2923,38 +2923,32 @@
  return getQuery
-String
 Parameters().get

Fix NPE when there are not incoming query string parameter and path parameter