Example usage for javax.servlet.http HttpServlet getServletContext

List of usage examples for javax.servlet.http HttpServlet getServletContext

Introduction

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

Prototype

public ServletContext getServletContext() 

Source Link

Document

Returns a reference to the ServletContext in which this servlet is running.

Usage

From source file:org.apache.axis.transport.http.AxisServletBase.java

/**
 * extract information from the servlet configuration files
 * @param servlet//from  w  ww  .j  av a 2s .  c o m
 * @return
 */
protected static Map getEngineEnvironment(HttpServlet servlet) {
    Map environment = new HashMap();

    String attdir = servlet.getInitParameter(AxisEngine.ENV_ATTACHMENT_DIR);
    if (attdir != null)
        environment.put(AxisEngine.ENV_ATTACHMENT_DIR, attdir);

    ServletContext context = servlet.getServletContext();
    environment.put(AxisEngine.ENV_SERVLET_CONTEXT, context);

    String webInfPath = context.getRealPath("/WEB-INF");
    if (webInfPath != null)
        environment.put(AxisEngine.ENV_SERVLET_REALPATH, webInfPath + File.separator + "attachments");

    EngineConfiguration config = EngineConfigurationFactoryFinder.newFactory(servlet).getServerEngineConfig();

    if (config != null) {
        environment.put(EngineConfiguration.PROPERTY_NAME, config);
    }

    return environment;
}

From source file:org.apache.tapestry.asset.AssetExternalizer.java

/**
 *  Gets the externalizer singleton for the application.  If it does not already
 *  exist, it is created and stored into the {@link ServletContext}.
 *
 *  <p>Each Tapestry application within a single {@link ServletContext}
 *  will have its own externalizer; they are differentiated by the
 *  application name./*from   w w  w  .  java  2 s  .c o  m*/
 *
 *  @see org.apache.tapestry.spec.ApplicationSpecification#getName()
 *
 **/

public static AssetExternalizer get(IRequestCycle cycle) {
    HttpServlet servlet = cycle.getRequestContext().getServlet();
    ServletContext context = servlet.getServletContext();

    String servletName = servlet.getServletName();

    String attributeName = "org.apache.tapestry.AssetExternalizer:" + servletName;

    AssetExternalizer result = (AssetExternalizer) context.getAttribute(attributeName);

    if (result == null) {
        result = new AssetExternalizer(cycle);
        context.setAttribute(attributeName, result);
    }

    return result;
}

From source file:org.apache.tapestry.engine.AbstractEngine.java

/**
 *  Invoked from {@link #service(RequestContext)} to ensure that the engine's
 *  instance variables are setup.  This allows the application a chance to
 *  restore transient variables that will not have survived deserialization.
 *
 *  Determines the servlet prefix:  this is the base URL used by
 *  {@link IEngineService services} to build URLs.  It consists
 *  of two parts:  the context path and the servlet path.
 *
 *  <p>The servlet path is retrieved from {@link HttpServletRequest#getServletPath()}.
 *
 *  <p>The context path is retrieved from {@link HttpServletRequest#getContextPath()}.
 *
 *  <p>The global object is retrieved from {@link IEngine#getGlobal()} method.
 *
 *  <p>The final path is available via the {@link #getServletPath()} method.
 *
 *  <p>In addition, this method locates and/or creates the:
 *  <ul>//  www .  jav  a2 s.c o  m
 *  <li>{@link IComponentClassEnhancer}
 *  <li>{@link Pool}
 *  <li>{@link ITemplateSource}
 *  <li>{@link ISpecificationSource}
 *  <li>{@link IPageSource}
 *  <li>{@link IEngineService} {@link Map}
 *  <ll>{@link IScriptSource}
 *  <li>{@link IComponentMessagesSource}
 *  <li>{@link IPropertySource}
 *  </ul>
 *
 *  <p>This order is important, because some of the later shared objects
 *  depend on some of the earlier shared objects already having
 *  been located or created
 *  (especially {@link #getPool() pool}).
 *
 *  <p>Subclasses should invoke this implementation first, then perform their
 *  own setup.
 *
 **/

protected void setupForRequest(RequestContext context) {
    HttpServlet servlet = context.getServlet();
    ServletContext servletContext = servlet.getServletContext();
    HttpServletRequest request = context.getRequest();
    HttpSession session = context.getSession();

    if (session != null)
        _sessionId = context.getSession().getId();
    else
        _sessionId = null;

    // Previously, this used getRemoteHost(), but that requires an
    // expensive reverse DNS lookup. Possibly, the host name lookup
    // should occur ... but only if there's an actual error message
    // to display.

    if (_clientAddress == null)
        _clientAddress = request.getRemoteAddr();

    // servletPath is null, so this means either we're doing the
    // first request in this session, or we're handling a subsequent
    // request in another JVM (i.e. another server in the cluster).
    // In any case, we have to do some late (re-)initialization.

    if (_servletPath == null) {
        // Get the path *within* the servlet context

        // In rare cases related to the tagsupport service, getServletPath() is wrong
        // (its a JSP, which invokes Tapestry as an include, thus muddling what
        // the real servlet and servlet path is).  In those cases, the JSP tag
        // will inform us.

        String path = (String) request.getAttribute(Tapestry.TAG_SUPPORT_SERVLET_PATH_ATTRIBUTE);

        if (path == null)
            path = request.getServletPath();

        // Get the context path, which may be the empty string
        // (but won't be null).

        _contextPath = request.getContextPath();

        _servletPath = _contextPath + path;
    }

    String servletName = context.getServlet().getServletName();

    if (_propertySource == null) {
        String name = PROPERTY_SOURCE_NAME + ":" + servletName;

        _propertySource = (IPropertySource) servletContext.getAttribute(name);

        if (_propertySource == null) {
            _propertySource = createPropertySource(context);

            servletContext.setAttribute(name, _propertySource);
        }
    }

    if (_enhancer == null) {
        String name = ENHANCER_NAME + ":" + servletName;

        _enhancer = (IComponentClassEnhancer) servletContext.getAttribute(name);

        if (_enhancer == null) {
            _enhancer = createComponentClassEnhancer(context);

            servletContext.setAttribute(name, _enhancer);
        }
    }

    if (_pool == null) {
        String name = POOL_NAME + ":" + servletName;

        _pool = (Pool) servletContext.getAttribute(name);

        if (_pool == null) {
            _pool = createPool(context);

            servletContext.setAttribute(name, _pool);
        }
    }

    if (_templateSource == null) {
        String name = TEMPLATE_SOURCE_NAME + ":" + servletName;

        _templateSource = (ITemplateSource) servletContext.getAttribute(name);

        if (_templateSource == null) {
            _templateSource = createTemplateSource(context);

            servletContext.setAttribute(name, _templateSource);
        }
    }

    if (_specificationSource == null) {
        String name = SPECIFICATION_SOURCE_NAME + ":" + servletName;

        _specificationSource = (ISpecificationSource) servletContext.getAttribute(name);

        if (_specificationSource == null) {
            _specificationSource = createSpecificationSource(context);

            servletContext.setAttribute(name, _specificationSource);
        }
    }

    if (_pageSource == null) {
        String name = PAGE_SOURCE_NAME + ":" + servletName;

        _pageSource = (IPageSource) servletContext.getAttribute(name);

        if (_pageSource == null) {
            _pageSource = createPageSource(context);

            servletContext.setAttribute(name, _pageSource);
        }
    }

    if (_scriptSource == null) {
        String name = SCRIPT_SOURCE_NAME + ":" + servletName;

        _scriptSource = (IScriptSource) servletContext.getAttribute(name);

        if (_scriptSource == null) {
            _scriptSource = createScriptSource(context);

            servletContext.setAttribute(name, _scriptSource);
        }
    }

    if (_serviceMap == null) {
        String name = SERVICE_MAP_NAME + ":" + servletName;

        _serviceMap = (Map) servletContext.getAttribute(name);

        if (_serviceMap == null) {
            _serviceMap = createServiceMap();

            servletContext.setAttribute(name, _serviceMap);
        }
    }

    if (_stringsSource == null) {
        String name = STRINGS_SOURCE_NAME + ":" + servletName;

        _stringsSource = (IComponentMessagesSource) servletContext.getAttribute(name);

        if (_stringsSource == null) {
            _stringsSource = createComponentStringsSource(context);

            servletContext.setAttribute(name, _stringsSource);
        }
    }

    if (_dataSqueezer == null) {
        String name = DATA_SQUEEZER_NAME + ":" + servletName;

        _dataSqueezer = (DataSqueezer) servletContext.getAttribute(name);

        if (_dataSqueezer == null) {
            _dataSqueezer = createDataSqueezer();

            servletContext.setAttribute(name, _dataSqueezer);
        }
    }

    if (_global == null) {
        String name = GLOBAL_NAME + ":" + servletName;

        _global = servletContext.getAttribute(name);

        if (_global == null) {
            _global = createGlobal(context);

            servletContext.setAttribute(name, _global);
        }
    }

    if (_resourceChecksumSource == null) {
        String name = RESOURCE_CHECKSUM_SOURCE_NAME + ":" + servletName;

        _resourceChecksumSource = (ResourceChecksumSource) servletContext.getAttribute(name);

        if (_resourceChecksumSource == null) {
            _resourceChecksumSource = createResourceChecksumSource();

            servletContext.setAttribute(name, _resourceChecksumSource);
        }
    }

    String encoding = request.getCharacterEncoding();
    if (encoding == null) {
        encoding = getOutputEncoding();
        try {
            request.setCharacterEncoding(encoding);
        } catch (UnsupportedEncodingException e) {
            throw new IllegalArgumentException(Tapestry.format("illegal-encoding", encoding));
        } catch (NoSuchMethodError e) {
            // Servlet API 2.2 compatibility
            // Behave okay if the setCharacterEncoding() method is unavailable
        } catch (AbstractMethodError e) {
            // Servlet API 2.2 compatibility
            // Behave okay if the setCharacterEncoding() method is unavailable
        }
    }
}

From source file:org.opendatakit.aggregate.ContextFactory.java

public static CallingContext getCallingContext(HttpServlet servlet, HttpServletRequest req) {
    return new CallingContextImpl(servlet.getServletContext(), req);
}

From source file:org.zoxweb.server.http.servlet.HTTPServletUtil.java

public static String inputStreamToString(HttpServlet servlet, String resource)
        throws NullPointerException, IOException {
    log.info("resouce:" + resource);
    String content = null;//w w w  .  j  a v a  2  s.  c o  m
    try {
        content = (IOUtil.inputStreamToString(servlet.getClass().getResourceAsStream(resource), true));
    } catch (Exception e) {

    }
    if (content == null) {
        ServletContext context = servlet.getServletContext();

        URL url = context.getResource(resource);
        log.info("url:" + url);
        content = (IOUtil.inputStreamToString(context.getResourceAsStream(resource), true));
    }

    return content;
}