org.ednovo.gooru.controller.ScreenshotController.java Source code

Java tutorial

Introduction

Here is the source code for org.ednovo.gooru.controller.ScreenshotController.java

Source

/*******************************************************************************
 * ScreenshotController.java
 * gooru-screenshot
 * Created by gooru-screenshot on 2014
 * Copyright (c) 2014 Gooru. All rights reserved.
 * http://www.goorulearning.org/
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES O     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE     AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 ******************************************************************************/
package org.ednovo.gooru.controller;

import java.net.URLDecoder;

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

import org.ednovo.gooru.web.util.WebDriverUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/rest")
public class ScreenshotController {

    @Autowired
    private WebDriverUtil webDriverUtil;

    private static final String ENCODE_FORMAT = "UTF-8";

    private static final String INVALID_URL = "Invalid URL";

    /**
     * Get and check parameters
     * 
     * @param sourceUrl
     *            (URL should be encoded)
     * @param width
     * @param height
     * @param request
     * @param response
     * @throws Exception
     */

    @RequestMapping(method = RequestMethod.GET, value = "/snapshot/generate/{width}/{height}")
    public void getScreenshot(@PathVariable(value = "width") int width, @PathVariable(value = "height") int height,
            @RequestParam(value = "SourceUrl", required = true) String sourceUrl, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        sourceUrl = URLDecoder.decode(sourceUrl, ENCODE_FORMAT);
        if (checkUrl(sourceUrl)) {
            response.setStatus(400);
            response.getWriter().append(INVALID_URL);
            return;
        } else {
            response.getOutputStream().write(getWebDriverUtil().generateScreenshot(sourceUrl, width, height));
        }

    }

    public WebDriverUtil getWebDriverUtil() {
        return webDriverUtil;
    }

    /* Check the URL is valid */
    public boolean checkUrl(String sourceUrl) {

        if (sourceUrl == null || sourceUrl == "") {
            return true;
        } else if (sourceUrl.startsWith("https://") || sourceUrl.startsWith("http://")) {
            return false;
        } else {
            return true;
        }

    }

}