/*
 * Copyright (c) WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. 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 org.wso2.carbon.mss.internal.router;

import io.netty.handler.codec.http.HttpRequest;
import org.wso2.carbon.mss.HttpResponder;

import java.util.List;
import java.util.Map;

/**
 * Container class that will hold HttpResourceModel and
 * request information inorder to process the request when all
 * data is ready.
 */
public class HttpMethodInfoBuilder {

    private HttpResourceModel httpResourceModel;
    private HttpRequest request;
    private HttpResponder responder;
    private Map<String, String> groupValues;
    private String contentType;
    private List<String> acceptTypes;

    public static HttpMethodInfoBuilder getInstance() {
        HttpMethodInfoBuilder httpMethodInfoBuilder = new HttpMethodInfoBuilder();
        return httpMethodInfoBuilder;
    }

    public HttpMethodInfoBuilder httpResourceModel(HttpResourceModel httpResourceModel) {
        this.httpResourceModel = httpResourceModel;
        return this;
    }

    public HttpMethodInfoBuilder httpRequest(HttpRequest request) {
        this.request = request;
        return this;
    }

    public HttpMethodInfoBuilder httpResponder(HttpResponder responder) {
        this.responder = responder;
        return this;
    }

    public HttpMethodInfoBuilder requestInfo(Map<String, String> groupValues,
                                             String contentType,
                                             List<String> acceptTypes) {
        this.groupValues = groupValues;
        this.contentType = contentType;
        this.acceptTypes = acceptTypes;
        return this;
    }

    public HttpMethodInfo build() throws HandlerException {
        return (new HttpResourceModelProcessor(httpResourceModel))
                .buildHttpMethodInfo(request,responder,groupValues,contentType,acceptTypes);
    }

    public HttpResourceModel getHttpResourceModel() {
        return httpResourceModel;
    }

    public HttpResponder getResponder() {
        return responder;
    }

    public HttpRequest getRequest() {
        return request;
    }
}

@@ -1258,16 +1258,59 @@
 ptTypes;
+%0A    private HttpMethodInfo httpMethodInfo;
 %0A%0A    pu
@@ -2355,22 +2355,74 @@
         
-return
+if (httpMethodInfo == null) %7B%0A            httpMethodInfo =
  (new Ht
@@ -2466,16 +2466,20 @@
 Model))%0A
+    
         
@@ -2515,16 +2515,17 @@
 request,
+ 
 responde
@@ -2526,16 +2526,17 @@
 sponder,
+ 
 groupVal
@@ -2539,16 +2539,17 @@
 pValues,
+ 
 contentT
@@ -2552,16 +2552,17 @@
 entType,
+ 
 acceptTy
@@ -2567,16 +2567,57 @@
 Types);%0A
+        %7D%0A        return httpMethodInfo;%0A
     %7D%0A%0A 

Build HttpMethodInfo instance only once