/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.url.internal.standard;

import javax.inject.Inject;
import javax.inject.Singleton;

import org.apache.commons.lang.StringUtils;
import org.xwiki.component.annotation.Component;
import org.xwiki.context.Execution;
import org.xwiki.context.ExecutionContext;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.internal.web.XWikiConfigurationService;
import com.xpn.xwiki.web.XWikiRequest;

/**
 * Replaces {@link DefaultStandardURLConfiguration} with fallbacks to configuration properties defined in the
 * {@code xwiki.cfg} configuration file.
 *
 * @version $Id$
 * @since 5.1M1
 */
@Component
@Singleton
public class LegacyStandardURLConfiguration extends DefaultStandardURLConfiguration
{
    @Inject
    private Execution execution;

    @Override
    public boolean isPathBasedMultiWiki()
    {
        return super.isPathBasedMultiWiki("1".equals(getLegacyValue("xwiki.virtual.usepath", "1")));
    }

    @Override
    public String getWikiPathPrefix()
    {
        return super.getWikiPathPrefix(getLegacyValue("xwiki.virtual.usepath.servletpath", "wiki"));
    }

    @Override
    public String getEntityPathPrefix()
    {
        String prefix = super.getWikiPathPrefix(getLegacyValue("xwiki.defaultservletpath", "bin"));

        // Remove potential trailing "/" since the documentation in xwiki.cfg says it should contain a trailing "/" but
        // getEntityPathPrefix should return the prefix without "/"...
        prefix = StringUtils.removeEnd(prefix, "/");

        return prefix;
    }

    @Override
    public WikiNotFoundBehavior getWikiNotFoundBehavior()
    {
        WikiNotFoundBehavior legacyBehavior = WikiNotFoundBehavior.DISPLAY_ERROR;

        String legacyValue = getLegacyValue("xwiki.virtual.failOnWikiDoesNotExist", "0");

        if (!"1".equals(legacyValue)) {
            legacyBehavior = WikiNotFoundBehavior.REDIRECT_TO_MAIN_WIKI;
        }

        return super.getWikiNotFoundBehavior(legacyBehavior);
    }

    private String getLegacyValue(String oldKey, String oldDefault)
    {
        String result = null;

        // Hack!
        // Get the ServletContext from the HTTP Session in case we're in a Servlet environment
        ExecutionContext econtext = this.execution.getContext();
        if (econtext != null) {
            XWikiContext xcontext = (XWikiContext) econtext.getProperty(XWikiContext.EXECUTIONCONTEXT_KEY);
            if (xcontext != null) {
                XWikiRequest request = xcontext.getRequest();
                if (request != null) {
                    result = XWikiConfigurationService.getProperty(oldKey, oldDefault,
                        request.getSession().getServletContext());
                }
            }
        }

        return result;
    }
}

@@ -957,16 +957,43 @@
 Inject;%0A
+import javax.inject.Named;%0A
 import j
@@ -1133,201 +1133,38 @@
 .con
-text.Execution;%0Aimport org.xwiki.context.ExecutionContext;%0A%0Aimport com.xpn.xwiki.XWikiContext;%0Aimport com.xpn.xwiki.internal.web.XWikiConfigurationService;%0Aimport com.xpn.xwiki.web.XWikiRequest
+figuration.ConfigurationSource
 ;%0A%0A/
@@ -1315,24 +1315,25 @@
 ion file.%0A *
+ 
 %0A * @version
@@ -1486,31 +1486,68 @@
     
-private Execution execu
+@Named(%22xwikicfg%22)%0A    private ConfigurationSource configura
 tion
@@ -1664,30 +1664,46 @@
 .equals(
-getLegacyValue
+this.configuration.getProperty
 (%22xwiki.
@@ -1828,38 +1828,54 @@
 iPathPrefix(
-getLegacyValue
+this.configuration.getProperty
 (%22xwiki.virt
@@ -2025,30 +2025,46 @@
 hPrefix(
-getLegacyValue
+this.configuration.getProperty
 (%22xwiki.
@@ -2559,30 +2559,46 @@
 Value = 
-getLegacyValue
+this.configuration.getProperty
 (%22xwiki.
@@ -2832,793 +2832,7 @@
    %7D
-%0A%0A    private String getLegacyValue(String oldKey, String oldDefault)%0A    %7B%0A        String result = null;%0A%0A        // Hack!%0A        // Get the ServletContext from the HTTP Session in case we're in a Servlet environment%0A        ExecutionContext econtext = this.execution.getContext();%0A        if (econtext != null) %7B%0A            XWikiContext xcontext = (XWikiContext) econtext.getProperty(XWikiContext.EXECUTIONCONTEXT_KEY);%0A            if (xcontext != null) %7B%0A                XWikiRequest request = xcontext.getRequest();%0A                if (request != null) %7B%0A                    result = XWikiConfigurationService.getProperty(oldKey, oldDefault,%0A                        request.getSession().getServletContext());%0A                %7D%0A            %7D%0A        %7D%0A%0A        return result;%0A    %7D
 %0A%7D%0A

Make xwiki.cfg accessible without XWiki instance