org.pegadi.webapp.webstart.WebstartController.java Source code

Java tutorial

Introduction

Here is the source code for org.pegadi.webapp.webstart.WebstartController.java

Source

/**
 * Copyright 1999-2009 The Pegadi Team
 *
 * 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 org.pegadi.webapp.webstart;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: ronnie
 * Date: Nov 17, 2005
 * Time: 1:01:57 PM
 * To change this template use File | Settings | File Templates.
 */
public class WebstartController extends AbstractController implements ResourceLoaderAware {

    private JnlpConfigBean configBean;

    private ResourceLoader resourceLoader;

    private final Logger log = LoggerFactory.getLogger(getClass());

    private ResourceBundle resourceBundle = ResourceBundle.getBundle("org.pegadi.webstart.webstart");

    protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest,
            HttpServletResponse httpServletResponse) throws Exception {

        Map<String, Object> model = new HashMap<String, Object>();
        List<String> libs = new ArrayList<String>();

        configBean.setVersion(resourceBundle.getString("version"));
        String contextPath = httpServletRequest.getContextPath();
        int serverport = httpServletRequest.getServerPort();
        String server = httpServletRequest.getServerName();
        configBean.setWebBase("http://" + server + ":" + serverport + contextPath);
        configBean.getProperties().put("server.host", server);
        File clientFolder = resourceLoader.getResource(configBean.getCodeBase()).getFile();

        if (clientFolder.isDirectory()) {
            File[] files = clientFolder.listFiles();
            for (File tempFile : files) {
                if (!tempFile.getName().contains(".jar")) {
                    continue;
                }
                if (tempFile.getName().contains(configBean.getMainLibBaseName())) {
                    model.put("main", configBean.getCodeBase() + tempFile.getName());
                } else {
                    libs.add(configBean.getCodeBase() + tempFile.getName());
                }
            }
        } else {
            log.error("ERROR: clientFolder " + clientFolder.getAbsolutePath() + " is not a directory");
            throw new ServletException("WebStartController: Could not read directory containing client jars.");
        }

        model.put("jars", libs);

        model.put("config", configBean);

        return new ModelAndView("webstart", model);
    }

    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @Required
    public void setConfigBean(JnlpConfigBean configBean) {
        this.configBean = configBean;
    }
}