/*
 * Copyright 2015 JBoss Inc
 *
 * 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.apiman.test.policies;

import io.apiman.gateway.engine.beans.ApiRequest;
import io.apiman.gateway.engine.beans.ApiResponse;
import io.apiman.test.common.mock.EchoResponse;

import java.io.IOException;
import java.util.Date;

import org.apache.commons.codec.digest.DigestUtils;
import org.codehaus.jackson.map.ObjectMapper;

/**
 * The default policy test backend API.
 *
 * @author eric.wittmann@redhat.com
 */
public class EchoBackEndApi implements IPolicyTestBackEndApi {

    private static final ObjectMapper mapper = new ObjectMapper();

    private static long counter = 0;

    /**
     * @see io.apiman.test.policies.IPolicyTestBackEndApi#invoke(io.apiman.gateway.engine.beans.ApiRequest, byte[])
     */
    @Override
    public PolicyTestBackEndApiResponse invoke(ApiRequest request, byte[] requestBody) {
        try {
            EchoResponse echoResponse = new EchoResponse();
            if (requestBody != null) {
                echoResponse.setBodyLength(new Long(requestBody.length));
                echoResponse.setBodySha1(DigestUtils.shaHex(requestBody));
            }
            echoResponse.setCounter(counter++);
            echoResponse.setHeaders(request.getHeaders());
            echoResponse.setMethod(request.getType());
            echoResponse.setResource(request.getDestination());
            echoResponse.setUri("urn:" + request.getDestination()); //$NON-NLS-1$

            ApiResponse apiResponse = new ApiResponse();
            apiResponse.setCode(200);
            apiResponse.setMessage("OK"); //$NON-NLS-1$
            apiResponse.getHeaders().put("Date", new Date().toString()); //$NON-NLS-1$
            apiResponse.getHeaders().put("Server", "apiman.policy-test"); //$NON-NLS-1$ //$NON-NLS-2$
            apiResponse.getHeaders().put("Content-Type", "application/json"); //$NON-NLS-1$ //$NON-NLS-2$

            String responseBody = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(echoResponse);
            apiResponse.getHeaders().put("Content-Length", String.valueOf(responseBody.length())); //$NON-NLS-1$

            PolicyTestBackEndApiResponse response = new PolicyTestBackEndApiResponse(apiResponse, responseBody);
            return response;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}

@@ -1041,16 +1041,42 @@
 m%0D%0A */%0D%0A
+@SuppressWarnings(%22nls%22)%0D%0A
 public c
@@ -2062,30 +2062,16 @@
 tion());
- //$NON-NLS-1$
 %0D%0A%0D%0A    
@@ -2204,30 +2204,16 @@
 e(%22OK%22);
- //$NON-NLS-1$
 %0D%0A      
@@ -2278,30 +2278,16 @@
 ring());
- //$NON-NLS-1$
 %0D%0A      
@@ -2357,36 +2357,8 @@
 t%22);
- //$NON-NLS-1$ //$NON-NLS-2$
 %0D%0A  
@@ -2436,36 +2436,8 @@
 n%22);
- //$NON-NLS-1$ //$NON-NLS-2$
 %0D%0A%0D%0A
@@ -2470,16 +2470,26 @@
 eBody = 
+normalize(
 mapper.w
@@ -2552,16 +2552,17 @@
 esponse)
+)
 ;%0D%0A     
@@ -2658,22 +2658,8 @@
 )));
- //$NON-NLS-1$
 %0D%0A%0D%0A
@@ -2901,11 +2901,205 @@
    %7D%0D%0A%0D%0A
+    /**%0D%0A     * Normalize newlines across platforms.%0D%0A     * @param output%0D%0A     */%0D%0A    private static String normalize(String output) %7B%0D%0A        return output.replace(%22%5Cr%5Cn%22, %22%5Cn%22);%0D%0A    %7D%0D%0A%0D%0A
 %7D%0D%0A

normalize the echo response (more consistent across platforms)