Example usage for javax.servlet ServletConfig getServletContext

List of usage examples for javax.servlet ServletConfig getServletContext

Introduction

In this page you can find the example usage for javax.servlet ServletConfig getServletContext.

Prototype

public ServletContext getServletContext();

Source Link

Document

Returns a reference to the ServletContext in which the caller is executing.

Usage

From source file:org.wso2.carbon.governance.generic.ui.utils.ManageGenericArtifactUtil.java

/**
   *  This method specifically creates a search service stub to do search by mediaType as the existing admin service uses a http
 *    request parameters to obtain the media type. But in this particular case the page gets directed from the component xml and
 *    hence cannot add pre defined request params there. And to keep the search admin service structure as it is, thus this bundle creates
 *    a separate client to access the search stub.
   */// w  w  w. j a  v  a  2  s  .  c om

public static List<InstalledRxt> getInstalledRxts(String cookie, ServletConfig config, HttpSession session)
        throws Exception {
    String backendServerURL = CarbonUIUtil.getServerURL(config.getServletContext(), session);
    ConfigurationContext configContext = (ConfigurationContext) config.getServletContext()
            .getAttribute(CarbonConstants.CONFIGURATION_CONTEXT);
    String epr = backendServerURL + "SearchAdminService";
    SearchAdminServiceStub stub;
    try {
        stub = new SearchAdminServiceStub(configContext, epr);

        ServiceClient client = stub._getServiceClient();
        Options option = client.getOptions();
        option.setManageSession(true);
        option.setProperty(org.apache.axis2.transport.http.HTTPConstants.COOKIE_STRING, cookie);

    } catch (AxisFault axisFault) {
        String msg = "Failed to initiate search service client. " + axisFault.getMessage();
        log.error(msg, axisFault);
        throw new Exception(msg, axisFault);
    }

    CustomSearchParameterBean searchQuery = new CustomSearchParameterBean();
    ArrayOfString arr = new ArrayOfString();
    arr.setArray(
            new String[] { "mediaType", GovernanceConstants.GOVERNANCE_ARTIFACT_CONFIGURATION_MEDIA_TYPE });
    ArrayOfString[] paramList = new ArrayOfString[] { arr };
    searchQuery.setParameterValues(paramList);
    AdvancedSearchResultsBean result = stub.getAdvancedSearchResults(searchQuery);
    ResourceData[] results = result.getResourceDataList();
    List<InstalledRxt> listInstalledRxts = new ArrayList<InstalledRxt>();

    if (results != null && results.length > 0) {
        for (ResourceData data : results) {
            String path = data.getResourcePath();
            if (path != null && path.contains("/")) {
                String rxt = path.substring(path.lastIndexOf("/") + 1).split("\\.")[0];
                InstalledRxt rxtObj = new InstalledRxt();
                rxtObj.setRxt(rxt);
                if (data.getDeleteAllowed()) {
                    rxtObj.setDeleteAllowed();
                }
                listInstalledRxts.add(rxtObj);
            }
        }
    }

    return listInstalledRxts;
}

From source file:org.wso2.carbon.hdfs.mgt.ui.GetHDFSItemContentProcessor.java

public static void getContent(HttpServletRequest request, HttpServletResponse response, ServletConfig config)
        throws Exception {

    try {//  w  w w.  java 2 s .com
        HDFSFileOperationAdminClient client = new HDFSFileOperationAdminClient(config.getServletContext(),
                request.getSession());
        String path = request.getParameter("path");
        if (path == null) {
            String msg = "Could not get the resource content. Path is not specified.";
            log.error(msg);
            response.setStatus(400);
            return;
        }

        HDFSFileContent fileContent = client.downloadFile(path);
        String fileName = path.substring(path.lastIndexOf("/") + 1, path.length());

        InputStream contentStream = null;
        if (fileContent.getDataHandler() != null) {
            contentStream = fileContent.getDataHandler().getInputStream();
        } else {
            String msg = "The resource content was empty.";
            log.error(msg);
            response.setStatus(204);
            return;
        }

        response.setContentType("application/download");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

        if (contentStream != null) {

            ServletOutputStream servletOutputStream = null;
            try {
                servletOutputStream = response.getOutputStream();

                byte[] contentChunk = new byte[1024];
                int byteCount;
                while ((byteCount = contentStream.read(contentChunk)) != -1) {
                    servletOutputStream.write(contentChunk, 0, byteCount);
                }

                response.flushBuffer();
                servletOutputStream.flush();

            } finally {
                contentStream.close();

                if (servletOutputStream != null) {
                    servletOutputStream.close();
                }
            }
        }
    } catch (HdfsMgtUiComponentException ex) {
        String msg = "Failed to get resource content. " + ex.getMessage();
        log.error(msg, ex);
        response.setStatus(500);
        return;

    } catch (RegistryException e) {
        String msg = "Failed to get resource content. " + e.getMessage();
        log.error(msg, e);
        response.setStatus(500);
        return;
    }
}

From source file:org.apache.axis.configuration.EngineConfigurationFactoryServlet.java

/**
 * Get a default server engine configuration in a servlet environment.
 *
 * @param ctx a ServletContext//  ww  w.ja  va  2 s. co m
 * @return a server EngineConfiguration
 */
private static EngineConfiguration getServerEngineConfig(ServletConfig cfg) {

    ServletContext ctx = cfg.getServletContext();

    // Respect the system property setting for a different config file
    String configFile = cfg.getInitParameter(OPTION_SERVER_CONFIG_FILE);
    if (configFile == null)
        configFile = AxisProperties.getProperty(OPTION_SERVER_CONFIG_FILE);
    if (configFile == null) {
        configFile = SERVER_CONFIG_FILE;
    }

    /**
     * Flow can be confusing.  Here is the logic:
     * 1) Make all attempts to open resource IF it exists
     *    - If it exists as a file, open as file (r/w)
     *    - If not a file, it may still be accessable as a stream (r)
     *    (env will handle security checks).
     * 2) If it doesn't exist, allow it to be opened/created
     * 
     * Now, the way this is done below is:
     * a) If the file does NOT exist, attempt to open as a stream (r)
     * b) Open named file (opens existing file, creates if not avail).
     */

    /*
     * Use the WEB-INF directory
     * (so the config files can't get snooped by a browser)
     */
    String appWebInfPath = "/WEB-INF";

    FileProvider config = null;

    String realWebInfPath = ctx.getRealPath(appWebInfPath);

    /**
     * If path/file doesn't exist, it may still be accessible
     * as a resource-stream (i.e. it may be packaged in a JAR
     * or WAR file).
     */
    if (realWebInfPath == null || !(new File(realWebInfPath, configFile)).exists()) {
        String name = appWebInfPath + "/" + configFile;
        InputStream is = ctx.getResourceAsStream(name);
        if (is != null) {
            // FileProvider assumes responsibility for 'is':
            // do NOT call is.close().
            config = new FileProvider(is);
        }

        if (config == null) {
            log.error(Messages.getMessage("servletEngineWebInfError03", name));
        }
    }

    /**
     * Couldn't get data  OR  file does exist.
     * If we have a path, then attempt to either open
     * the existing file, or create an (empty) file.
     */
    if (config == null && realWebInfPath != null) {
        try {
            config = new FileProvider(realWebInfPath, configFile);
        } catch (ConfigurationException e) {
            log.error(Messages.getMessage("servletEngineWebInfError00"), e);
        }
    }

    /**
     * Fall back to config file packaged with AxisEngine
     */
    if (config == null) {
        log.warn(Messages.getMessage("servletEngineWebInfWarn00"));
        try {
            InputStream is = ClassUtils.getResourceAsStream(AxisServer.class, SERVER_CONFIG_FILE);
            config = new FileProvider(is);
        } catch (Exception e) {
            log.error(Messages.getMessage("servletEngineWebInfError02"), e);
        }
    }

    return config;
}

From source file:edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties.java

public static ConfigurationProperties getBean(ServletConfig servletConfig) {
    if (servletConfig == null) {
        throw new NullPointerException("servletConfig may not be null.");
    }//from   w  ww  .  j  a  v  a 2  s . c  o  m
    return getBean(servletConfig.getServletContext());
}

From source file:org.wso2.carbon.ndatasource.ui.NDataSourceAdminServiceClient.java

public static NDataSourceAdminServiceClient getInstance(ServletConfig config, HttpSession session)
        throws AxisFault {
    String backendServerURL = CarbonUIUtil.getServerURL(config.getServletContext(), session);
    ConfigurationContext configContext = (ConfigurationContext) config.getServletContext()
            .getAttribute(CarbonConstants.CONFIGURATION_CONTEXT);

    String cookie = (String) session.getAttribute(ServerConstants.ADMIN_SERVICE_COOKIE);
    return new NDataSourceAdminServiceClient(cookie, backendServerURL, configContext);

}

From source file:org.wso2.carbon.hdfs.mgt.ui.HDFSTreeProcessor.java

public static String process(HttpServletRequest request, HttpServletResponse response, ServletConfig config,
        String resourcePath, String parentId) throws UIException {

    HDFSAdminClient client;/*from  ww  w  .  ja v a 2 s .  co  m*/
    try {
        client = new HDFSAdminClient(config.getServletContext(), request.getSession());
    } catch (Exception e) {
        String msg = "Failed to initialize the resource service client " + "to get resource tree data. "
                + e.getMessage();
        log.error(msg, e);
        throw new UIException(msg, e);
    }

    String textBoxId = request.getParameter("textBoxId");
    try {
        HDFSTreeData resourceTreeData = new HDFSTreeData();
        fillSubResourceTree(resourcePath, resourceTreeData, client, textBoxId, parentId,
                request.getParameter("hideResources") != null);

        String displayHTML = "";
        displayHTML += resourceTreeData.getResourceTree();
        return displayHTML;

    } catch (Exception e) {
        String msg = e.getMessage();
        if (msg == null || msg == "") {
            msg = "Failed to generate the resource tree for the resource " + resourcePath + ". "
                    + e.getMessage();
        }
        log.error(msg, e);
        throw new UIException(msg, e);
    }

}

From source file:org.wso2.carbon.task.ui.internal.TaskManagementClient.java

public static TaskManagementClient getInstance(ServletConfig config, HttpSession session)
        throws TaskManagementException, AxisFault {

    String backendServerURL = CarbonUIUtil.getServerURL(config.getServletContext(), session);
    ConfigurationContext configContext = (ConfigurationContext) config.getServletContext()
            .getAttribute(CarbonConstants.CONFIGURATION_CONTEXT);

    String cookie = (String) session.getAttribute(ServerConstants.ADMIN_SERVICE_COOKIE);
    return new TaskManagementClient(cookie, backendServerURL, configContext);
}

From source file:org.wso2.carbon.datasource.ui.DatasourceManagementClient.java

public static DatasourceManagementClient getInstance(ServletConfig config, HttpSession session)
        throws AxisFault, DataSourceManagementException {

    String backendServerURL = CarbonUIUtil.getServerURL(config.getServletContext(), session);
    ConfigurationContext configContext = (ConfigurationContext) config.getServletContext()
            .getAttribute(CarbonConstants.CONFIGURATION_CONTEXT);

    String cookie = (String) session.getAttribute(ServerConstants.ADMIN_SERVICE_COOKIE);
    return new DatasourceManagementClient(cookie, backendServerURL, configContext);
}

From source file:org.wso2.carbon.ui.CarbonUIUtil.java

public static boolean isContextRegistered(ServletConfig config, String context) {
    URL url;/*  w ww .  j av  a 2 s  .  co m*/
    try {
        url = config.getServletContext().getResource(context);
    } catch (MalformedURLException e) {
        return false;
    }
    if (url == null) {
        return false;
    } else if (url.toString().indexOf(context) != -1) {
        return true;
    }
    return false;
}

From source file:com.icesoft.faces.async.render.RenderManager.java

public static synchronized void setServletConfig(final ServletConfig servletConfig) {

    if (internalRenderManager == null) {
        internalRenderManager = new InternalRenderManager(servletConfig.getServletContext());
    }// www.  jav a 2  s.  com
}