Example usage for com.liferay.portal.kernel.webdav WebDAVStorage setToken

List of usage examples for com.liferay.portal.kernel.webdav WebDAVStorage setToken

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.webdav WebDAVStorage setToken.

Prototype

public void setToken(String token);

Source Link

Usage

From source file:com.liferay.document.library.webdav.test.BaseWebDAVTestCase.java

License:Open Source License

public Tuple service(String method, String path, Map<String, String> headers, byte[] data) {

    WebDAVStorage webDAVStorage = new DLWebDAVStorageImpl();

    webDAVStorage.setToken("document_library");

    WebDAVUtil.addStorage(webDAVStorage);

    WebDAVServlet webDAVServlet = new WebDAVServlet();

    String requestURI = _CONTEXT_PATH + _SERVLET_PATH + _PATH_INFO_PREFACE + path;

    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(method, requestURI);

    mockHttpServletRequest.setContextPath(_CONTEXT_PATH);
    mockHttpServletRequest.setServletPath(_SERVLET_PATH);
    mockHttpServletRequest.setPathInfo(_PATH_INFO_PREFACE + path);

    try {//from  w  ww  .  j av  a2s  .  com
        mockHttpServletRequest.setRemoteUser(String.valueOf(TestPropsValues.getUserId()));
    } catch (Exception e) {
        Assert.fail("User ID cannot be initialized");
    }

    if (headers == null) {
        headers = new HashMap<>();
    }

    headers.put(HttpHeaders.USER_AGENT, getUserAgent());

    try {
        throw new Exception();
    } catch (Exception e) {
        StackTraceElement[] stackTraceElements = e.getStackTrace();

        for (StackTraceElement stackTraceElement : stackTraceElements) {
            String methodName = stackTraceElement.getMethodName();

            if (methodName.equals("setUp") || methodName.equals("tearDown") || methodName.startsWith("test")) {

                String testName = StringUtil.extractLast(stackTraceElement.getClassName(), CharPool.PERIOD);

                testName = StringUtil.removeSubstrings(testName, "WebDAV", "Test");

                headers.put("X-Litmus", testName + ": (" + stackTraceElement.getMethodName() + ":"
                        + stackTraceElement.getLineNumber() + ")");

                break;
            }
        }
    }

    if (data != null) {
        mockHttpServletRequest.setContent(data);

        String contentType = headers.remove(HttpHeaders.CONTENT_TYPE);

        if (contentType != null) {
            mockHttpServletRequest.setContentType(contentType);
        } else {
            mockHttpServletRequest.setContentType(ContentTypes.TEXT_PLAIN);
        }
    }

    for (Map.Entry<String, String> entry : headers.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();

        mockHttpServletRequest.addHeader(key, value);
    }

    try {
        MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();

        webDAVServlet.service(mockHttpServletRequest, mockHttpServletResponse);

        int statusCode = mockHttpServletResponse.getStatus();
        byte[] responseBody = mockHttpServletResponse.getContentAsByteArray();

        Map<String, String> responseHeaders = new HashMap<>();

        for (String name : mockHttpServletResponse.getHeaderNames()) {
            responseHeaders.put(name, mockHttpServletResponse.getHeader(name));
        }

        return new Tuple(statusCode, responseBody, responseHeaders);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.liferay.portlet.PortletBagFactory.java

License:Open Source License

public PortletBag create(Portlet portlet) throws Exception {
    PortletApp portletApp = portlet.getPortletApp();

    if (!portletApp.isWARFile() && _warFile) {
        String contextPath = PortalUtil.getPathContext();

        _servletContext = ServletContextPool.get(contextPath);

        _classLoader = PortalClassLoaderUtil.getClassLoader();
    }//from ww  w  .  j a  v  a 2s. c o  m

    Class<?> portletClass = null;

    try {
        portletClass = _classLoader.loadClass(portlet.getPortletClass());
    } catch (Throwable e) {
        _log.error(e, e);

        PortletLocalServiceUtil.destroyPortlet(portlet);

        return null;
    }

    javax.portlet.Portlet portletInstance = (javax.portlet.Portlet) portletClass.newInstance();

    ConfigurationAction configurationActionInstance = newConfigurationAction(portlet);

    List<Indexer> indexerInstances = newIndexers(portlet);

    OpenSearch openSearchInstance = newOpenSearch(portlet);

    initSchedulers(portlet);

    FriendlyURLMapper friendlyURLMapperInstance = newFriendlyURLMapper(portlet);

    URLEncoder urlEncoderInstance = newURLEncoder(portlet);

    PortletDataHandler portletDataHandlerInstance = newPortletDataHandler(portlet);

    PortletLayoutListener portletLayoutListenerInstance = newPortletLayoutListener(portlet);

    PollerProcessor pollerProcessorInstance = newPollerProcessor(portlet);

    MessageListener popMessageListenerInstance = newPOPMessageListener(portlet);

    SocialActivityInterpreter socialActivityInterpreterInstance = initSocialActivityInterpreterInstance(
            portlet);

    SocialRequestInterpreter socialRequestInterpreterInstance = null;

    if (Validator.isNotNull(portlet.getSocialRequestInterpreterClass())) {
        socialRequestInterpreterInstance = (SocialRequestInterpreter) newInstance(
                SocialRequestInterpreter.class, portlet.getSocialRequestInterpreterClass());

        socialRequestInterpreterInstance = new SocialRequestInterpreterImpl(portlet.getPortletId(),
                socialRequestInterpreterInstance);

        SocialRequestInterpreterLocalServiceUtil.addRequestInterpreter(socialRequestInterpreterInstance);
    }

    WebDAVStorage webDAVStorageInstance = null;

    if (Validator.isNotNull(portlet.getWebDAVStorageClass())) {
        webDAVStorageInstance = (WebDAVStorage) newInstance(WebDAVStorage.class,
                portlet.getWebDAVStorageClass());

        webDAVStorageInstance.setToken(portlet.getWebDAVStorageToken());

        WebDAVUtil.addStorage(webDAVStorageInstance);
    }

    Method xmlRpcMethodInstance = null;

    if (Validator.isNotNull(portlet.getXmlRpcMethodClass())) {
        xmlRpcMethodInstance = (Method) newInstance(Method.class, portlet.getXmlRpcMethodClass());

        XmlRpcServlet.registerMethod(xmlRpcMethodInstance);
    }

    ControlPanelEntry controlPanelEntryInstance = null;

    if (Validator.isNotNull(portlet.getControlPanelEntryClass())) {
        controlPanelEntryInstance = (ControlPanelEntry) newInstance(ControlPanelEntry.class,
                portlet.getControlPanelEntryClass());
    }

    List<AssetRendererFactory> assetRendererFactoryInstances = newAssetRendererFactoryInstances(portlet);

    List<AtomCollectionAdapter<?>> atomCollectionAdapterInstances = newAtomCollectionAdapterInstances(portlet);

    List<CustomAttributesDisplay> customAttributesDisplayInstances = new ArrayList<CustomAttributesDisplay>();

    for (String customAttributesDisplayClass : portlet.getCustomAttributesDisplayClasses()) {

        CustomAttributesDisplay customAttributesDisplayInstance = (CustomAttributesDisplay) newInstance(
                CustomAttributesDisplay.class, customAttributesDisplayClass);

        customAttributesDisplayInstance
                .setClassNameId(PortalUtil.getClassNameId(customAttributesDisplayInstance.getClassName()));
        customAttributesDisplayInstance.setPortletId(portlet.getPortletId());

        customAttributesDisplayInstances.add(customAttributesDisplayInstance);
    }

    PermissionPropagator permissionPropagatorInstance = newPermissionPropagator(portlet);

    List<WorkflowHandler> workflowHandlerInstances = new ArrayList<WorkflowHandler>();

    for (String workflowHandlerClass : portlet.getWorkflowHandlerClasses()) {

        WorkflowHandler workflowHandlerInstance = (WorkflowHandler) newInstance(WorkflowHandler.class,
                workflowHandlerClass);

        workflowHandlerInstances.add(workflowHandlerInstance);

        WorkflowHandlerRegistryUtil.register(workflowHandlerInstance);
    }

    PreferencesValidator preferencesValidatorInstance = null;

    if (Validator.isNotNull(portlet.getPreferencesValidator())) {
        preferencesValidatorInstance = (PreferencesValidator) newInstance(PreferencesValidator.class,
                portlet.getPreferencesValidator());

        try {
            if (PropsValues.PREFERENCE_VALIDATE_ON_STARTUP) {
                preferencesValidatorInstance.validate(
                        PortletPreferencesFactoryUtil.fromDefaultXML(portlet.getDefaultPreferences()));
            }
        } catch (Exception e) {
            _log.warn("Portlet with the name " + portlet.getPortletId()
                    + " does not have valid default preferences");
        }
    }

    Map<String, ResourceBundle> resourceBundles = null;

    String resourceBundle = portlet.getResourceBundle();

    if (Validator.isNotNull(resourceBundle) && !resourceBundle.equals(StrutsResourceBundle.class.getName())) {

        resourceBundles = new HashMap<String, ResourceBundle>();

        initResourceBundle(resourceBundles, portlet, null);
        initResourceBundle(resourceBundles, portlet, LocaleUtil.getDefault());

        Set<String> supportedLocales = portlet.getSupportedLocales();

        if (supportedLocales.isEmpty()) {
            supportedLocales = SetUtil.fromArray(PropsValues.LOCALES);
        }

        for (String supportedLocale : supportedLocales) {
            Locale locale = LocaleUtil.fromLanguageId(supportedLocale);

            initResourceBundle(resourceBundles, portlet, locale);
        }
    }

    PortletBag portletBag = new PortletBagImpl(portlet.getPortletId(), _servletContext, portletInstance,
            configurationActionInstance, indexerInstances, openSearchInstance, friendlyURLMapperInstance,
            urlEncoderInstance, portletDataHandlerInstance, portletLayoutListenerInstance,
            pollerProcessorInstance, popMessageListenerInstance, socialActivityInterpreterInstance,
            socialRequestInterpreterInstance, webDAVStorageInstance, xmlRpcMethodInstance,
            controlPanelEntryInstance, assetRendererFactoryInstances, atomCollectionAdapterInstances,
            customAttributesDisplayInstances, permissionPropagatorInstance, workflowHandlerInstances,
            preferencesValidatorInstance, resourceBundles);

    PortletBagPool.put(portlet.getRootPortletId(), portletBag);

    try {
        PortletInstanceFactoryUtil.create(portlet, _servletContext);
    } catch (Exception e) {
        _log.error(e, e);
    }

    return portletBag;
}