com.formkiq.web.config.TestDataController.java Source code

Java tutorial

Introduction

Here is the source code for com.formkiq.web.config.TestDataController.java

Source

/*
 * Copyright (C) 2016 FormKiQ 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 com.formkiq.web.config;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.formkiq.core.util.Resources;

/**
 * Sending Static Test Data.
 *
 */
@Controller
public class TestDataController {

    /**
     * Provider Sample generic XML data with text/xml content type.
     * @param request {@link HttpServletRequest}
     * @param response {@link HttpServletResponse}
     * @throws IOException IOException
     */
    @RequestMapping(value = "/testdata/sample-generic1.xml", method = RequestMethod.GET)
    public void sampleGeneric1(final HttpServletRequest request, final HttpServletResponse response)
            throws IOException {

        byte[] data = Resources.getResourceAsBytes("/testdata/sample-generic.xml");

        response.setContentType("text/xml");
        response.setContentLengthLong(data.length);
        IOUtils.write(data, response.getOutputStream());
    }

    /**
     * Provider Sample generic XML data with text/xml content type.
     * @param request {@link HttpServletRequest}
     * @param response {@link HttpServletResponse}
     * @throws IOException IOException
     */
    @RequestMapping(value = "/testdata/sample-generic2.xml", method = RequestMethod.GET)
    public void sampleGeneric2(final HttpServletRequest request, final HttpServletResponse response)
            throws IOException {

        byte[] data = Resources.getResourceAsBytes("/testdata/sample-generic.xml");

        response.setContentType("application/xml");
        response.setContentLengthLong(data.length);
        IOUtils.write(data, response.getOutputStream());
    }

    /**
     * Provider Sample generic XML data with text/xml content type.
     * @param request {@link HttpServletRequest}
     * @param response {@link HttpServletResponse}
     * @throws IOException IOException
     */
    @RequestMapping(value = "/testdata/sample-generic1.json", method = RequestMethod.GET)
    public void sampleGeneric3(final HttpServletRequest request, final HttpServletResponse response)
            throws IOException {

        byte[] data = Resources.getResourceAsBytes("/testdata/sample-generic.json");

        response.setContentType("application/json");
        response.setContentLengthLong(data.length);
        IOUtils.write(data, response.getOutputStream());
    }
}