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