Search in sources :

Example 1 with WOResourceManager

use of com.webobjects.appserver.WOResourceManager in project wonder-slim by undur.

the class ERXResponseRewriter method addResourceInHead.

/**
 * Adds a reference to an arbitrary file with a correct resource URL wrapped
 * between startTag and endTag in the HTML head tag if it isn't already
 * present in the response.
 *
 * @param response
 *            the response
 * @param context
 *            the context
 * @param framework
 *            the framework that contains the file
 * @param fileName
 *            the name of the file to add
 * @param startTag
 *            the HTML to prepend before the URL
 * @param endTag
 *            the HTML to append after the URL
 * @param fallbackStartTag
 * @param fallbackEndTag
 * @param tagMissingBehavior
 *            how to handle the case where the tag is missing
 *
 * @return whether or not the content was added
 */
public static boolean addResourceInHead(WOResponse response, WOContext context, String framework, String fileName, String startTag, String endTag, String fallbackStartTag, String fallbackEndTag, TagMissingBehavior tagMissingBehavior) {
    boolean inserted = true;
    String replacementResourceStr = ERXProperties.stringForKey("er.extensions.ERXResponseRewriter.resource." + framework + "." + fileName);
    if (replacementResourceStr != null) {
        int dotIndex = replacementResourceStr.indexOf('.');
        framework = replacementResourceStr.substring(0, dotIndex);
        fileName = replacementResourceStr.substring(dotIndex + 1);
    }
    if (!ERXResponseRewriter.isResourceAddedToHead(context, framework, fileName) && (_delagate == null || _delagate.responseRewriterShouldAddResource(framework, fileName))) {
        boolean insert = true;
        if (_delagate != null) {
            Resource replacementResource = _delagate.responseRewriterWillAddResource(framework, fileName);
            if (replacementResource != null) {
                framework = replacementResource.framework();
                fileName = replacementResource.fileName();
                // double-check that the replacement hasn't already been added
                if (ERXResponseRewriter.isResourceAddedToHead(context, framework, fileName)) {
                    insert = false;
                }
            }
        }
        if (insert) {
            String url;
            if (fileName.indexOf("://") != -1 || fileName.startsWith("/")) {
                url = fileName;
            } else {
                WOResourceManager rm = WOApplication.application().resourceManager();
                NSArray languages = null;
                if (context.hasSession()) {
                    languages = context.session().languages();
                }
                url = rm.urlForResourceNamed(fileName, framework, languages, context.request());
                boolean generateCompleteResourceURLs = ERXResourceManager._shouldGenerateCompleteResourceURL(context);
                boolean secureAllResources = ERXProperties.booleanForKey(ERXResponseRewriter.SECURE_RESOURCES_KEY) && !ERXRequest.isRequestSecure(context.request());
                if (generateCompleteResourceURLs || secureAllResources) {
                    url = ERXResourceManager._completeURLForResource(url, secureAllResources ? Boolean.TRUE : null, context);
                }
            }
            String html = startTag + url + endTag + "\n";
            if (fallbackStartTag == null && fallbackEndTag == null) {
                inserted = ERXResponseRewriter.insertInResponseBeforeHead(response, context, html, tagMissingBehavior);
            } else {
                inserted = ERXResponseRewriter.insertInResponseBeforeHead(response, context, html, TagMissingBehavior.Skip);
                if (!inserted) {
                    String fallbackHtml = fallbackStartTag + url + fallbackEndTag + "\n";
                    inserted = ERXResponseRewriter.insertInResponseBeforeTag(response, context, fallbackHtml, null, TagMissingBehavior.Top);
                }
            }
            if (inserted) {
                ERXResponseRewriter.resourceAddedToHead(context, framework, fileName);
            }
        }
    }
    return inserted;
}
Also used : WOResourceManager(com.webobjects.appserver.WOResourceManager) NSArray(com.webobjects.foundation.NSArray)

Example 2 with WOResourceManager

use of com.webobjects.appserver.WOResourceManager in project wonder-slim by undur.

the class ERXStaticResourceRequestHandler method handleRequest.

@Override
public WOResponse handleRequest(WORequest request) {
    WOResponse response = null;
    InputStream is = null;
    long length = 0;
    String contentType = null;
    String uri = request.uri();
    if (uri.charAt(0) == '/') {
        WOResourceManager rm = application.resourceManager();
        String documentRoot = documentRoot();
        File file = null;
        StringBuilder sb = new StringBuilder(documentRoot.length() + uri.length());
        String wodataKey = request.stringFormValueForKey("wodata");
        if (uri.startsWith("/cgi-bin") && wodataKey != null) {
            uri = wodataKey;
            if (uri.startsWith("file:")) {
                // remove file:/
                uri = uri.substring(5);
            } else {
            }
        } else {
            int index = uri.indexOf("/wodata=");
            if (index >= 0) {
                uri = uri.substring(index + "/wodata=".length());
            } else {
                sb.append(documentRoot);
            }
        }
        if (_useRequestHandlerPath) {
            try {
                WODynamicURL dynamicURL = new WODynamicURL(uri);
                String requestHandlerPath = dynamicURL.requestHandlerPath();
                if (requestHandlerPath == null || requestHandlerPath.length() == 0) {
                    sb.append(uri);
                } else {
                    sb.append('/');
                    sb.append(requestHandlerPath);
                }
            } catch (Exception e) {
                throw new RuntimeException("Failed to parse URL '" + uri + "'.", e);
            }
        } else {
            sb.append(uri);
        }
        String path = sb.toString();
        try {
            path = path.replaceAll("\\?.*", "");
            if (request.userInfo() != null && !request.userInfo().containsKey("HttpServletRequest")) {
                /* PATH_INFO is already decoded by the servlet container */
                path = path.replace('+', ' ');
                path = URLDecoder.decode(path, StandardCharsets.UTF_8);
            }
            file = new File(path);
            if (path.startsWith("jar:")) {
                URLConnection uc = new URL(path).openConnection();
                if (uc instanceof JarURLConnection) {
                    length = (int) uc.getContentLengthLong();
                } else {
                    length = -1;
                }
                is = uc.getInputStream();
            } else {
                length = (int) file.length();
                is = new FileInputStream(file);
            }
            contentType = rm.contentTypeForResourceNamed(path);
            log.debug("Reading file '{}' for uri: {}", file, uri);
        } catch (IOException ex) {
            if (!uri.toLowerCase().endsWith("/favicon.ico")) {
                log.info("Unable to get contents of file '{}' for uri: {}", file, uri);
            }
        }
    } else {
        log.error("Can't fetch relative path: {}", uri);
    }
    response = _generateResponseForInputStream(is, length, contentType);
    NSNotificationCenter.defaultCenter().postNotification(WORequestHandler.DidHandleRequestNotification, response);
    response._finalizeInContext(null);
    return response;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JarURLConnection(java.net.JarURLConnection) WODynamicURL(com.webobjects.appserver.WODynamicURL) IOException(java.io.IOException) IOException(java.io.IOException) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) URL(java.net.URL) WODynamicURL(com.webobjects.appserver.WODynamicURL) FileInputStream(java.io.FileInputStream) WOResourceManager(com.webobjects.appserver.WOResourceManager) WOResponse(com.webobjects.appserver.WOResponse) File(java.io.File)

Example 3 with WOResourceManager

use of com.webobjects.appserver.WOResourceManager in project wonder-slim by undur.

the class ERXComponentUtilities method templateUrl.

/**
 * Returns the URL of the template for the given component name.
 *
 * @param componentName
 *            the name of the component to load a template for (without the
 *            .wo)
 * @param extension
 *            the file extension of the template (without the dot -- i.e.
 *            "html")
 * @param languages
 *            the list of languages to use for finding components
 * @return the URL to the template (or null if there isn't one)
 */
public static URL templateUrl(String componentName, String extension, NSArray languages) {
    String htmlPathName = componentName + ".wo/" + componentName + "." + extension;
    WOResourceManager resourceManager = WOApplication.application().resourceManager();
    URL templateUrl = pathUrlForResourceNamed(resourceManager, htmlPathName, languages);
    if (templateUrl == null) {
        // jw: hack for bundle-less builds as there is some sort of classpath problem that will
        // pick up the wrong class for WOProjectBundle, _WOProject, … that register only component's
        // .wo directories but not the containing files (.html, .wod, .woo). Thus we are assuming
        // that if we can find the .wo directory we can manually point to the correct subfile
        templateUrl = pathUrlForResourceNamed(resourceManager, componentName + ".wo", languages);
        if (templateUrl != null) {
            File templateDir = null;
            try {
                templateDir = new File(templateUrl.toURI());
            } catch (URISyntaxException e) {
                templateDir = new File(templateUrl.getPath());
            }
            if (templateDir.isDirectory()) {
                File templateFile = new File(templateDir, componentName + "." + extension);
                if (templateFile.exists()) {
                    try {
                        templateUrl = templateFile.toURI().toURL();
                    } catch (MalformedURLException e) {
                    // ignore
                    }
                }
            }
        }
    }
    return templateUrl;
}
Also used : MalformedURLException(java.net.MalformedURLException) WOResourceManager(com.webobjects.appserver.WOResourceManager) URISyntaxException(java.net.URISyntaxException) File(java.io.File) URL(java.net.URL)

Aggregations

WOResourceManager (com.webobjects.appserver.WOResourceManager)3 File (java.io.File)2 URL (java.net.URL)2 WODynamicURL (com.webobjects.appserver.WODynamicURL)1 WOResponse (com.webobjects.appserver.WOResponse)1 NSArray (com.webobjects.foundation.NSArray)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 JarURLConnection (java.net.JarURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 URLConnection (java.net.URLConnection)1