001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.taglib.ui;
016    
017    import com.liferay.portal.kernel.editor.Editor;
018    import com.liferay.portal.kernel.editor.configuration.EditorConfiguration;
019    import com.liferay.portal.kernel.editor.configuration.EditorConfigurationFactoryUtil;
020    import com.liferay.portal.kernel.language.LanguageUtil;
021    import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
022    import com.liferay.portal.kernel.servlet.PortalWebResourcesUtil;
023    import com.liferay.portal.kernel.util.JavaConstants;
024    import com.liferay.portal.kernel.util.MapUtil;
025    import com.liferay.portal.kernel.util.PropsKeys;
026    import com.liferay.portal.kernel.util.PropsUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.kernel.util.WebKeys;
029    import com.liferay.portal.model.Portlet;
030    import com.liferay.portal.theme.ThemeDisplay;
031    import com.liferay.portlet.RequestBackedPortletURLFactory;
032    import com.liferay.portlet.RequestBackedPortletURLFactoryUtil;
033    import com.liferay.registry.Registry;
034    import com.liferay.registry.RegistryUtil;
035    import com.liferay.registry.ServiceReference;
036    import com.liferay.registry.collections.ServiceReferenceMapper;
037    import com.liferay.registry.collections.ServiceTrackerCollections;
038    import com.liferay.registry.collections.ServiceTrackerMap;
039    import com.liferay.taglib.aui.AUIUtil;
040    import com.liferay.taglib.util.IncludeTag;
041    import com.liferay.taglib.util.TagResourceBundleUtil;
042    
043    import java.io.IOException;
044    
045    import java.util.Enumeration;
046    import java.util.HashMap;
047    import java.util.Map;
048    import java.util.ResourceBundle;
049    
050    import javax.portlet.PortletRequest;
051    import javax.portlet.PortletResponse;
052    
053    import javax.servlet.ServletException;
054    import javax.servlet.http.HttpServletRequest;
055    import javax.servlet.http.HttpServletResponse;
056    
057    /**
058     * @author Brian Wing Shun Chan
059     */
060    public class InputEditorTag extends IncludeTag {
061    
062            public void setAllowBrowseDocuments(boolean allowBrowseDocuments) {
063                    _allowBrowseDocuments = allowBrowseDocuments;
064            }
065    
066            public void setAutoCreate(boolean autoCreate) {
067                    _autoCreate = autoCreate;
068            }
069    
070            public void setConfigKey(String configKey) {
071                    _configKey = configKey;
072            }
073    
074            public void setConfigParams(Map<String, String> configParams) {
075                    _configParams = configParams;
076            }
077    
078            public void setContents(String contents) {
079                    _contents = contents;
080            }
081    
082            public void setContentsLanguageId(String contentsLanguageId) {
083                    _contentsLanguageId = contentsLanguageId;
084            }
085    
086            public void setCssClass(String cssClass) {
087                    _cssClass = cssClass;
088            }
089    
090            public void setData(Map<String, Object> data) {
091                    _data = data;
092            }
093    
094            /**
095             * @deprecated As of 7.0.0, replaced by {@link #setEditorName(String)}
096             */
097            @Deprecated
098            public void setEditorImpl(String editorImpl) {
099                    _editorName = PropsUtil.get(editorImpl);
100            }
101    
102            public void setEditorName(String editorName) {
103                    _editorName = editorName;
104            }
105    
106            public void setFileBrowserParams(Map<String, String> fileBrowserParams) {
107                    _fileBrowserParams = fileBrowserParams;
108            }
109    
110            public void setHeight(String height) {
111                    _height = height;
112            }
113    
114            /**
115             * @deprecated As of 7.0.0, replaced by {@link #setContents(String)}
116             */
117            @Deprecated
118            public void setInitMethod(String initMethod) {
119                    _initMethod = initMethod;
120            }
121    
122            public void setInlineEdit(boolean inlineEdit) {
123                    _inlineEdit = inlineEdit;
124            }
125    
126            public void setInlineEditSaveURL(String inlineEditSaveURL) {
127                    _inlineEditSaveURL = inlineEditSaveURL;
128            }
129    
130            public void setName(String name) {
131                    _name = name;
132            }
133    
134            public void setOnBlurMethod(String onBlurMethod) {
135                    _onBlurMethod = onBlurMethod;
136            }
137    
138            public void setOnChangeMethod(String onChangeMethod) {
139                    _onChangeMethod = onChangeMethod;
140            }
141    
142            public void setOnFocusMethod(String onFocusMethod) {
143                    _onFocusMethod = onFocusMethod;
144            }
145    
146            public void setOnInitMethod(String onInitMethod) {
147                    _onInitMethod = onInitMethod;
148            }
149    
150            public void setPlaceholder(String placeholder) {
151                    _placeholder = placeholder;
152            }
153    
154            public void setResizable(boolean resizable) {
155                    _resizable = resizable;
156            }
157    
158            public void setShowSource(boolean showSource) {
159                    _showSource = showSource;
160            }
161    
162            public void setSkipEditorLoading(boolean skipEditorLoading) {
163                    _skipEditorLoading = skipEditorLoading;
164            }
165    
166            public void setToolbarSet(String toolbarSet) {
167                    _toolbarSet = toolbarSet;
168            }
169    
170            public void setWidth(String width) {
171                    _width = width;
172            }
173    
174            @Override
175            protected void cleanUp() {
176                    _allowBrowseDocuments = true;
177                    _autoCreate = true;
178                    _configKey = null;
179                    _configParams = null;
180                    _contents = null;
181                    _contentsLanguageId = null;
182                    _cssClass = null;
183                    _data = null;
184                    _editorName = null;
185                    _fileBrowserParams = null;
186                    _height = null;
187                    _initMethod = "initEditor";
188                    _inlineEdit = false;
189                    _inlineEditSaveURL = null;
190                    _name = "editor";
191                    _onChangeMethod = null;
192                    _onBlurMethod = null;
193                    _onFocusMethod = null;
194                    _onInitMethod = null;
195                    _placeholder = null;
196                    _resizable = true;
197                    _showSource = true;
198                    _skipEditorLoading = false;
199                    _toolbarSet = _TOOLBAR_SET_DEFAULT;
200                    _width = null;
201            }
202    
203            protected String getConfigKey() {
204                    String configKey = _configKey;
205    
206                    if (Validator.isNull(configKey)) {
207                            configKey = _name;
208                    }
209    
210                    return configKey;
211            }
212    
213            protected String getContentsLanguageId() {
214                    if (_contentsLanguageId == null) {
215                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
216                                    WebKeys.THEME_DISPLAY);
217    
218                            _contentsLanguageId = themeDisplay.getLanguageId();
219                    }
220    
221                    return _contentsLanguageId;
222            }
223    
224            protected String getCssClasses() {
225                    Portlet portlet = (Portlet)request.getAttribute(WebKeys.RENDER_PORTLET);
226    
227                    String cssClasses = "portlet ";
228    
229                    if (portlet != null) {
230                            cssClasses += portlet.getCssClassWrapper();
231                    }
232    
233                    return cssClasses;
234            }
235    
236            protected Map<String, Object> getData() {
237                    String portletId = (String)request.getAttribute(WebKeys.PORTLET_ID);
238    
239                    if (portletId == null) {
240                            return _data;
241                    }
242    
243                    Map<String, Object> attributes = new HashMap<>();
244    
245                    Enumeration<String> enumeration = request.getAttributeNames();
246    
247                    while (enumeration.hasMoreElements()) {
248                            String attributeName = enumeration.nextElement();
249    
250                            if (attributeName.startsWith("liferay-ui:input-editor")) {
251                                    attributes.put(
252                                            attributeName, request.getAttribute(attributeName));
253                            }
254                    }
255    
256                    attributes.put("liferay-ui:input-editor:namespace", getNamespace());
257    
258                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
259                            WebKeys.THEME_DISPLAY);
260    
261                    EditorConfiguration editorConfiguration =
262                            EditorConfigurationFactoryUtil.getEditorConfiguration(
263                                    portletId, getConfigKey(), getEditorName(request), attributes,
264                                    themeDisplay, getRequestBackedPortletURLFactory());
265    
266                    Map<String, Object> data = editorConfiguration.getData();
267    
268                    if (MapUtil.isNotEmpty(_data)) {
269                            MapUtil.merge(_data, data);
270                    }
271    
272                    return data;
273            }
274    
275            protected Editor getEditor(HttpServletRequest request) {
276                    String editorName = _editorName;
277    
278                    if (!BrowserSnifferUtil.isRtf(request)) {
279                            return _serviceTrackerMap.getService("simple");
280                    }
281    
282                    if (Validator.isNull(editorName)) {
283                            return _serviceTrackerMap.getService(_EDITOR_WYSIWYG_DEFAULT);
284                    }
285    
286                    if (!_serviceTrackerMap.containsKey(editorName)) {
287                            return _serviceTrackerMap.getService(_EDITOR_WYSIWYG_DEFAULT);
288                    }
289    
290                    return _serviceTrackerMap.getService(editorName);
291            }
292    
293            protected String getEditorName(HttpServletRequest request) {
294                    Editor editor = getEditor(request);
295    
296                    return editor.getName();
297            }
298    
299            protected String getEditorResourceType() {
300                    Editor editor = getEditor(request);
301    
302                    return editor.getResourceType();
303            }
304    
305            protected String getNamespace() {
306                    PortletRequest portletRequest = (PortletRequest)request.getAttribute(
307                            JavaConstants.JAVAX_PORTLET_REQUEST);
308                    PortletResponse portletResponse = (PortletResponse)request.getAttribute(
309                            JavaConstants.JAVAX_PORTLET_RESPONSE);
310    
311                    if ((portletRequest == null) || (portletResponse == null)) {
312                            return AUIUtil.getNamespace(request);
313                    }
314    
315                    return AUIUtil.getNamespace(portletRequest, portletResponse);
316            }
317    
318            @Override
319            protected String getPage() {
320                    Editor editor = getEditor(request);
321    
322                    return editor.getJspPath();
323            }
324    
325            protected RequestBackedPortletURLFactory
326                    getRequestBackedPortletURLFactory() {
327    
328                    PortletRequest portletRequest = (PortletRequest)request.getAttribute(
329                            JavaConstants.JAVAX_PORTLET_REQUEST);
330    
331                    if (portletRequest == null) {
332                            return RequestBackedPortletURLFactoryUtil.create(request);
333                    }
334    
335                    return RequestBackedPortletURLFactoryUtil.create(portletRequest);
336            }
337    
338            protected String getToolbarSet() {
339                    if (Validator.isNotNull(_toolbarSet)) {
340                            return _toolbarSet;
341                    }
342    
343                    return _TOOLBAR_SET_DEFAULT;
344            }
345    
346            @Override
347            protected void includePage(String page, HttpServletResponse response)
348                    throws IOException, ServletException {
349    
350                    servletContext = PortalWebResourcesUtil.getServletContext(
351                            getEditorResourceType());
352    
353                    super.includePage(page, response);
354            }
355    
356            @Override
357            protected void setAttributes(HttpServletRequest request) {
358                    request.setAttribute(
359                            "liferay-ui:input-editor:allowBrowseDocuments",
360                            String.valueOf(_allowBrowseDocuments));
361                    request.setAttribute(
362                            "liferay-ui:input-editor:autoCreate", String.valueOf(_autoCreate));
363                    request.setAttribute(
364                            "liferay-ui:input-editor:configParams", _configParams);
365                    request.setAttribute("liferay-ui:input-editor:contents", _contents);
366                    request.setAttribute(
367                            "liferay-ui:input-editor:contentsLanguageId",
368                            getContentsLanguageId());
369                    request.setAttribute("liferay-ui:input-editor:cssClass", _cssClass);
370                    request.setAttribute(
371                            "liferay-ui:input-editor:cssClasses", getCssClasses());
372                    request.setAttribute(
373                            "liferay-ui:input-editor:editorName", getEditorName(request));
374                    request.setAttribute(
375                            "liferay-ui:input-editor:fileBrowserParams", _fileBrowserParams);
376                    request.setAttribute("liferay-ui:input-editor:height", _height);
377                    request.setAttribute("liferay-ui:input-editor:initMethod", _initMethod);
378                    request.setAttribute(
379                            "liferay-ui:input-editor:inlineEdit", String.valueOf(_inlineEdit));
380                    request.setAttribute(
381                            "liferay-ui:input-editor:inlineEditSaveURL", _inlineEditSaveURL);
382                    request.setAttribute("liferay-ui:input-editor:name", _name);
383                    request.setAttribute(
384                            "liferay-ui:input-editor:onBlurMethod", _onBlurMethod);
385                    request.setAttribute(
386                            "liferay-ui:input-editor:onChangeMethod", _onChangeMethod);
387                    request.setAttribute(
388                            "liferay-ui:input-editor:onFocusMethod", _onFocusMethod);
389                    request.setAttribute(
390                            "liferay-ui:input-editor:onInitMethod", _onInitMethod);
391    
392                    ResourceBundle resourceBundle = TagResourceBundleUtil.getResourceBundle(
393                            pageContext);
394    
395                    if (Validator.isNull(_placeholder)) {
396                            _placeholder = LanguageUtil.get(
397                                    resourceBundle, "write-your-content-here");
398                    }
399    
400                    request.setAttribute(
401                            "liferay-ui:input-editor:placeholder", _placeholder);
402    
403                    request.setAttribute(
404                            "liferay-ui:input-editor:resizable", String.valueOf(_resizable));
405                    request.setAttribute(
406                            "liferay-ui:input-editor:showSource", String.valueOf(_showSource));
407                    request.setAttribute(
408                            "liferay-ui:input-editor:skipEditorLoading",
409                            String.valueOf(_skipEditorLoading));
410                    request.setAttribute(
411                            "liferay-ui:input-editor:toolbarSet", getToolbarSet());
412                    request.setAttribute("liferay-ui:input-editor:width", _width);
413    
414                    request.setAttribute("liferay-ui:input-editor:data", getData());
415            }
416    
417            private static final String _EDITOR_WYSIWYG_DEFAULT = PropsUtil.get(
418                    PropsKeys.EDITOR_WYSIWYG_DEFAULT);
419    
420            private static final String _TOOLBAR_SET_DEFAULT = "liferay";
421    
422            private static final ServiceTrackerMap<String, Editor> _serviceTrackerMap =
423                    ServiceTrackerCollections.singleValueMap(
424                            Editor.class, null,
425                            new ServiceReferenceMapper<String, Editor>() {
426    
427                                    @Override
428                                    public void map(
429                                            ServiceReference<Editor> serviceReference,
430                                            Emitter<String> emitter) {
431    
432                                            Registry registry = RegistryUtil.getRegistry();
433    
434                                            Editor editor = registry.getService(serviceReference);
435    
436                                            emitter.emit(editor.getName());
437                                    }
438    
439                            });
440    
441            static {
442                    _serviceTrackerMap.open();
443            }
444    
445            private boolean _allowBrowseDocuments = true;
446            private boolean _autoCreate = true;
447            private String _configKey;
448            private Map<String, String> _configParams;
449            private String _contents;
450            private String _contentsLanguageId;
451            private String _cssClass;
452            private Map<String, Object> _data = null;
453            private String _editorName;
454            private Map<String, String> _fileBrowserParams;
455            private String _height;
456            private String _initMethod = "initEditor";
457            private boolean _inlineEdit;
458            private String _inlineEditSaveURL;
459            private String _name = "editor";
460            private String _onBlurMethod;
461            private String _onChangeMethod;
462            private String _onFocusMethod;
463            private String _onInitMethod;
464            private String _placeholder;
465            private boolean _resizable = true;
466            private boolean _showSource = true;
467            private boolean _skipEditorLoading;
468            private String _toolbarSet = _TOOLBAR_SET_DEFAULT;
469            private String _width;
470    
471    }