org.apache.felix.webconsole.internal.deppack.DepPackServlet.java Source code

Java tutorial

Introduction

Here is the source code for org.apache.felix.webconsole.internal.deppack.DepPackServlet.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.deppack;

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

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

import org.apache.commons.fileupload.FileItem;
import org.apache.felix.webconsole.AbstractWebConsolePluginServlet;
import org.apache.felix.webconsole.internal.BaseWebConsolePluginServlet;
import org.apache.felix.webconsole.internal.Util;
import org.apache.felix.webconsole.internal.servlet.OsgiManager;
import org.json.JSONException;
import org.json.JSONWriter;
import org.osgi.service.component.ComponentContext;
//import org.osgi.service.deploymentadmin.DeploymentAdmin;
//import org.osgi.service.deploymentadmin.DeploymentException;
//import org.osgi.service.deploymentadmin.DeploymentPackage;

public class DepPackServlet extends BaseWebConsolePluginServlet {

    public static final String PACK_SERV_LABEL = "deppack";

    public static final String TITLE = "Deployment Packages";

    private static final String PACK_ACTION_DEPLOY = "deploydp";

    private static final String PACK_ACTION_UNINSTALL = "uninstalldp";

    private static final String PARAMETER_PCK_FILE = "pckfile";

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

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

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // get the uploaded data
        final String currAction = getRequestParameter(request, Util.UTIL_ACTION);
        if (PACK_ACTION_DEPLOY.equals(currAction)) {
            Map currParams = (Map) request.getAttribute(AbstractWebConsolePluginServlet.ATTR_FILEUPLOAD);
            if (currParams != null) {
                final FileItem fileItem = getFileItem(currParams, PARAMETER_PCK_FILE, false);
                throw new ServletException("Deploy package failed."); // yanlin:
                // remove
                // this
                // final DeploymentAdmin admin = ( DeploymentAdmin )
                // this.getService( DeploymentAdmin.class.getName() );
                // if ( admin != null )
                // {
                // try
                // {
                // admin.installDeploymentPackage( pck.getInputStream() );
                //
                // final String uri = req.getRequestURI();
                // resp.sendRedirect( uri );
                // return;
                // }
                // catch ( DeploymentException e )
                // {
                // throw new ServletException( "Unable to deploy package.", e );
                // }
                // }
            }
            throw new ServletException("Upload file or deployment admin missing.");
        } else if (PACK_ACTION_UNINSTALL.equals(currAction)) {
            final String pckId = request.getPathInfo().substring(request.getPathInfo().lastIndexOf('/') + 1);
            if (pckId != null && pckId.length() > 0) {
                throw new ServletException("Deploy package failed.");// codegerm:
                // remove
                // this
                // final DeploymentAdmin admin = ( DeploymentAdmin )
                // this.getService( DeploymentAdmin.class.getName() );
                // if ( admin != null )
                // {
                // try
                // {
                // final DeploymentPackage pck = admin.getDeploymentPackage(
                // pckId );
                // if ( pck != null )
                // {
                // pck.uninstall();
                // }
                // }
                // catch ( DeploymentException e )
                // {
                // throw new ServletException( "Unable to undeploy package.", e
                // );
                // }
                // }

            }

            final PrintWriter printWriter = response.getWriter();
            printWriter.println("{ \"reload\":true }");
            return;
        }
        throw new ServletException("The action [" + currAction + "] is invalid");
    }

    private FileItem getFileItem(Map paramsMap, String name, boolean isFormField) {
        FileItem[] items = (FileItem[]) paramsMap.get(name);
        if (items != null) {
            for (int i = 0; i < items.length; i++) {
                if (items[i].isFormField() == isFormField) {
                    return items[i];
                }
            }
        }

        return null;
    }

    public String getServletCapital() {
        return TITLE;
    }

    public String getServletLabel() {
        return PACK_SERV_LABEL;
    }

    private void jsonKeyVal(JSONWriter jsonWriter, String key, Object value) throws JSONException {
        if (key != null && value != null) {
            jsonWriter.object();
            jsonWriter.key("key");
            jsonWriter.value(key);
            jsonWriter.key("value");
            jsonWriter.value(value);
            jsonWriter.endObject();
        }
    }

    // private void packageInfoJson( JSONWriter jw, DeploymentPackage pack )
    // throws JSONException
    // {
    // jw.object();
    // jw.key( "id" );
    // jw.value( pack.getName() );
    // jw.key( "name" );
    // jw.value( pack.getName() );
    // jw.key( "state" );
    // jw.value( pack.getVersion() );
    //
    // jw.key( "actions" );
    // jw.array();
    //
    // jw.object();
    // jw.key( "enabled" );
    // jw.value( true );
    // jw.key( "name" );
    // jw.value( "Uninstall" );
    // jw.key( "link" );
    // jw.value( ACTION_UNINSTALL );
    // jw.endObject();
    //
    // jw.endArray();
    //
    // jw.key( "props" );
    // jw.array();
    // keyVal( jw, "Package Name", pack.getName() );
    // keyVal( jw, "Version", pack.getVersion() );
    //
    // final StringBuffer buffer = new StringBuffer();
    // for ( int i = 0; i < pack.getBundleInfos().length; i++ )
    // {
    // buffer.append( pack.getBundleInfos()[i].getSymbolicName() );
    // buffer.append( " - " );
    // buffer.append( pack.getBundleInfos()[i].getVersion() );
    // buffer.append( "<br/>" );
    // }
    // keyVal( jw, "Bundles", buffer.toString() );
    //
    // jw.endArray();
    //
    // jw.endObject();
    // }

    protected void renderServletRequest(HttpServletRequest comingRequest, HttpServletResponse response)
            throws ServletException, IOException {

        PrintWriter printWriter = response.getWriter();

        String appRoot = (String) comingRequest.getAttribute(OsgiManager.OSGI_APP_ROOT);
        printWriter.println("<script src='" + appRoot + "/res/ui/packages.js' language='JavaScript'></script>");

        printWriter.println("<h1>Deployment Admin</h1>");
        // final DeploymentAdmin admin = ( DeploymentAdmin ) this.getService(
        // DeploymentAdmin.class.getName() );
        // if ( admin == null )
        // {
        // pw.println( "<p><em>Deployment Admin is not installed.</em></p>" );
        // return;
        // }
        // final DeploymentPackage[] packages = admin.listDeploymentPackages();
        //
        // Util.startScript( pw );
        // pw.println( "var packageListData = " );
        // JSONWriter jw = new JSONWriter( pw );
        // try
        // {
        // jw.object();
        //
        // jw.key( "data" );
        //
        // jw.array();
        //
        // for ( int i = 0; i < packages.length; i++ )
        // {
        // packageInfoJson( jw, packages[i] );
        // }
        //
        // jw.endArray();
        //
        // jw.endObject();
        //
        // }
        // catch ( JSONException je )
        // {
        // throw new IOException( je.toString() );
        // }

        printWriter.println(";\nrenderPackage( packageListData );");
        Util.endScript(printWriter);
    }
}