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