com.formkiq.core.api.FeedsController.java Source code

Java tutorial

Introduction

Here is the source code for com.formkiq.core.api.FeedsController.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.core.api;

import java.io.IOException;
import java.util.List;
import java.util.Map;

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

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.formkiq.core.service.feed.FeedService;
import com.formkiq.core.service.feed.InvalidFeedUrlException;
import com.formkiq.core.util.Strings;

/**
 * Feed Controller handles actions.
 *
 */
@RestController
public class FeedsController extends AbstractRestController {

    /** Feeds Get. */
    public static final String API_FEEDS_GET = "/api/feeds/get";

    /** FeedService. */
    @Autowired
    private FeedService feedService;

    /**
     * Gets a Feed and transfers XML to JSON.
     * @param request {@link HttpServletRequest}
     * @param response {@link HttpServletResponse}
     * @throws IOException IOException
     * @throws InvalidFeedUrlException InvalidFeedUrlException
     */
    @RequestMapping(value = API_FEEDS_GET)
    public void get(final HttpServletRequest request, final HttpServletResponse response)
            throws IOException, InvalidFeedUrlException {

        getApiVersion(request);

        Map<String, String[]> map = request.getParameterMap();

        String url = getParameter(map, "url", true).trim();

        ResponseEntity<String> entity = this.feedService.get(url);

        String body = entity.getBody();

        if (this.feedService.isXMLContentType(entity.getHeaders())) {

            body = this.feedService.toJSONFromXML(body);

        } else if (!this.feedService.isJSONContentType(entity.getHeaders())) {

            List<String> contentTypes = entity.getHeaders().get("content-type");
            String ct = !contentTypes.isEmpty() ? contentTypes.get(0) : "unknown content type";
            throw new InvalidFeedUrlException("Invalid content-type " + ct + " for " + url);
        }

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