org.apache.felix.webconsole.internal.misc.LicenseServlet.java Source code

Java tutorial

Introduction

Here is the source code for org.apache.felix.webconsole.internal.misc.LicenseServlet.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.felix.webconsole.internal.misc;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.URL;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

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

import org.apache.felix.webconsole.AbstractWebConsolePluginServlet;
import org.apache.felix.webconsole.internal.OsgiManagerPlugin;
import org.apache.felix.webconsole.internal.Util;
import org.json.JSONException;
import org.json.JSONWriter;
import org.osgi.framework.Bundle;
import org.osgi.service.component.ComponentContext;

public class LicenseServlet extends AbstractWebConsolePluginServlet implements OsgiManagerPlugin {

    private static final int URL_FORWARD = 1;
    private static final String ISO_STRING = "ISO-8859-1";

    protected void activate(ComponentContext context) {
        activateBundle(context.getBundleContext());
    }

    protected void deactivate(ComponentContext context) {
        deActivate();
    }

    private void findResource(JSONWriter jsonWriter, Bundle bundle, String[] patterns)
            throws IOException, JSONException {
        jsonWriter.key("Bundle Resources"); // aka the bundle files
        jsonWriter.array();
        for (int i = 0; i < patterns.length; i++) {
            Enumeration getEntries = bundle.findEntries("/", patterns[i] + "*", true);
            if (getEntries != null) {
                while (getEntries.hasMoreElements()) {
                    URL nextURL = (URL) getEntries.nextElement();
                    jsonWriter.object();
                    jsonWriter.key("url");
                    jsonWriter.value(getName(nextURL.getPath()));
                    jsonWriter.key("data");
                    jsonWriter.value(readResourceFromURL(nextURL));
                    jsonWriter.endObject();
                }
            }
        }
        jsonWriter.endArray();

        Enumeration jarEntries = bundle.findEntries("/", "*.jar", true);
        if (jarEntries != null) {
            while (jarEntries.hasMoreElements()) {
                URL nextURL = (URL) jarEntries.nextElement();

                jsonWriter.key("Embedded " + getName(nextURL.getPath()));
                jsonWriter.array();

                for (int i = 0; i < patterns.length; i++) {
                    String patternStuff = ".*/" + patterns[i] + "[^/]*$";

                    InputStream tmpInput = null;
                    try {
                        tmpInput = nextURL.openStream();
                        ZipInputStream zipInput = new ZipInputStream(tmpInput);
                        ZipEntry zipEntry = zipInput.getNextEntry();
                        while (zipEntry != null) {
                            String name = zipEntry.getName();
                            if (!name.endsWith("/") && "/".concat(name).matches(patternStuff)) {
                                jsonWriter.object();
                                jsonWriter.key("url");
                                jsonWriter.value(getName(name));
                                jsonWriter.key("data");
                                jsonWriter.value(readResourceFromStream(new FilterInputStream(zipInput) {
                                    public void close() {
                                    }
                                }));
                                jsonWriter.endObject();
                            }

                            zipEntry = zipInput.getNextEntry();
                        }
                    } finally {
                        if (tmpInput != null) {
                            try {
                                tmpInput.close();
                            } catch (IOException ignore) {
                            }
                        }
                    }
                }

                jsonWriter.endArray();
            }
        }
    }

    private String getName(String origPath) {
        return origPath.substring(origPath.lastIndexOf('/') + URL_FORWARD);
    }

    private String getResource(Bundle bundle, String[] path) throws IOException {
        for (int i = 0; i < path.length; i++) {
            URL bundleResource = bundle.getResource(path[i]);
            if (bundleResource != null) {
                return readResourceFromURL(bundleResource);
            }
        }

        return null;
    }

    public String getServletCapital() {
        return "Licenses";
    }

    public String getServletLabel() {
        return "licenses";
    }

    private String readResourceFromStream(InputStream resource) throws IOException {
        Reader resourceReader = null;
        StringBuffer strBuffer = new StringBuffer();
        try {
            char[] tmpBuf = new char[1024];
            resourceReader = new InputStreamReader(resource, ISO_STRING);
            int tmpNum;
            while ((tmpNum = resourceReader.read(tmpBuf)) >= 0) {
                strBuffer.append(tmpBuf, 0, tmpNum);
            }
        } finally {
            if (resourceReader != null) {
                try {
                    resourceReader.close();
                } catch (IOException ignore) {
                }
            }
        }
        return strBuffer.toString();
    }

    private String readResourceFromURL(URL resource) throws IOException {
        return readResourceFromStream(resource.openStream());
    }

    protected void renderServletRequest(HttpServletRequest req, HttpServletResponse res) throws IOException {
        PrintWriter printWriter = res.getWriter();

        String tmpAppRootString = req.getContextPath() + req.getServletPath();
        printWriter.println(
                "<link href='" + tmpAppRootString + "/res/ui/license.css' rel='stylesheet' type='text/css'>");
        printWriter.println(
                "<script src='" + tmpAppRootString + "/res/ui/license.js' language='JavaScript'></script>");

        Bundle[] currBundles = getBundleContext().getBundles();
        Util.sortBundleList(currBundles);

        Util.startScript(printWriter);
        printWriter.print("bundleData = ");
        JSONWriter jsonWriter = new JSONWriter(printWriter);
        try {
            jsonWriter.object();
            for (int i = 0; i < currBundles.length; i++) {
                Bundle bundle = currBundles[i];
                jsonWriter.key(String.valueOf(bundle.getBundleId()));

                jsonWriter.object();

                jsonWriter.key("title");
                jsonWriter.value(Util.getBundleName(bundle));

                jsonWriter.key("files");
                jsonWriter.object();
                findResource(jsonWriter, bundle, new String[] { "README", "DISCLAIMER", "LICENSE", "NOTICE" });
                jsonWriter.endObject();

                jsonWriter.endObject();
            }
            jsonWriter.endObject();
            printWriter.println(";");
        } catch (JSONException jsonEx) {
            throw new IOException(jsonEx.toString());
        }
        Util.endScript(printWriter);

        printWriter.println("<div id='licenseContent'>");

        printWriter.println("<div id='licenseLeft'>");
        for (int i = 0; i < currBundles.length; i++) {
            Bundle curBundle = currBundles[i];
            String tmpLink = "displayBundle( \"" + curBundle.getBundleId() + "\" );";
            printWriter.println(
                    "<a href='javascript:" + tmpLink + "'>" + Util.getBundleName(curBundle) + "</a><br />");

        }
        printWriter.println("</div>");

        printWriter.println("<div id='licenseRight'>");
        printWriter.println("<div id='licenseButtons' class='licenseButtons'>&nbsp;</div>");
        printWriter.println("<br />");
        printWriter.println("<div id='licenseDetails' class='licenseDetails'>&nbsp;</div>");
        printWriter.println("</div>");

        printWriter.println("<div id='licenseClear'>&nbsp;</div>");

        printWriter.println("</div>"); // licenseContent

        Util.startScript(printWriter);
        printWriter.println("displayBundle( '0' );");
        Util.endScript(printWriter);
    }
}