001    /**
002     * Copyright (c) 2000-2012 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.portal.action;
016    
017    import com.liferay.portal.kernel.json.JSONArray;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
021    import com.liferay.portal.kernel.util.Constants;
022    import com.liferay.portal.kernel.util.ContentTypes;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.license.util.LicenseManagerUtil;
026    import com.liferay.portal.license.util.LicenseUtil;
027    import com.liferay.portal.model.User;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portlet.admin.util.OmniadminUtil;
030    
031    import java.util.List;
032    import java.util.Map;
033    
034    import javax.servlet.http.HttpServletRequest;
035    import javax.servlet.http.HttpServletResponse;
036    
037    import org.apache.struts.action.Action;
038    import org.apache.struts.action.ActionForm;
039    import org.apache.struts.action.ActionForward;
040    import org.apache.struts.action.ActionMapping;
041    
042    /**
043     * @author Amos Fong
044     */
045    public class UpdateLicenseAction extends Action {
046    
047            @Override
048            public ActionForward execute(
049                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
050                            HttpServletResponse response)
051                    throws Exception {
052    
053                    // PLACEHOLDER 01
054                    // PLACEHOLDER 02
055                    // PLACEHOLDER 03
056                    // PLACEHOLDER 04
057                    // PLACEHOLDER 05
058                    // PLACEHOLDER 06
059                    // PLACEHOLDER 07
060                    // PLACEHOLDER 08
061    
062                    if (_isValidRequest(request)) {
063                            String cmd = ParamUtil.getString(request, Constants.CMD);
064    
065                            String clusterNodeId = ParamUtil.getString(
066                                    request, "clusterNodeId");
067    
068                            if (cmd.equals("licenseProperties")) {
069                                    String licenseProperties = _getLicenseProperties(clusterNodeId);
070    
071                                    response.setContentType(ContentTypes.TEXT_JAVASCRIPT);
072    
073                                    ServletResponseUtil.write(response, licenseProperties);
074    
075                                    return null;
076                            }
077                            else if (cmd.equals("serverInfo")) {
078                                    String serverInfo = _getServerInfo(clusterNodeId);
079    
080                                    response.setContentType(ContentTypes.TEXT_JAVASCRIPT);
081    
082                                    ServletResponseUtil.write(response, serverInfo);
083    
084                                    return null;
085                            }
086    
087                            return mapping.findForward("portal.license");
088                    }
089                    else {
090                            response.sendRedirect(
091                                    PortalUtil.getPathContext() + "/c/portal/layout");
092    
093                            return null;
094                    }
095            }
096    
097            private String _getLicenseProperties(String clusterNodeId) {
098                    List<Map<String, String>> licenseProperties =
099                            LicenseManagerUtil.getClusterLicenseProperties(clusterNodeId);
100    
101                    if (licenseProperties == null) {
102                            return StringPool.BLANK;
103                    }
104    
105                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
106    
107                    for (Map<String, String> propertiesMap : licenseProperties) {
108                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
109    
110                            for (Map.Entry<String, String> entry : propertiesMap.entrySet()) {
111                                    jsonObject.put(entry.getKey(), entry.getValue());
112                            }
113    
114                            jsonArray.put(jsonObject);
115                    }
116    
117                    return jsonArray.toString();
118            }
119    
120            private String _getServerInfo(String clusterNodeId) throws Exception {
121                    Map<String, String> serverInfo = LicenseUtil.getClusterServerInfo(
122                            clusterNodeId);
123    
124                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
125    
126                    if (serverInfo != null) {
127                            for (Map.Entry<String, String> entry : serverInfo.entrySet()) {
128                                    jsonObject.put(entry.getKey(), entry.getValue());
129                            }
130                    }
131    
132                    return jsonObject.toString();
133            }
134    
135            private boolean _isOmniAdmin(HttpServletRequest request) {
136                    User user = null;
137    
138                    try {
139                            user = PortalUtil.getUser(request);
140                    }
141                    catch (Exception e) {
142                    }
143    
144                    if ((user != null) && OmniadminUtil.isOmniadmin(user.getUserId())) {
145                            return true;
146                    }
147                    else {
148                            return false;
149                    }
150            }
151    
152            private boolean _isValidRequest(HttpServletRequest request) {
153    
154                    // PLACEHOLDER 09
155                    // PLACEHOLDER 10
156                    // PLACEHOLDER 11
157                    // PLACEHOLDER 12
158                    // PLACEHOLDER 13
159                    // PLACEHOLDER 14
160                    // PLACEHOLDER 15
161                    // PLACEHOLDER 16
162                    // PLACEHOLDER 17
163                    // PLACEHOLDER 18
164                    // PLACEHOLDER 19
165                    // PLACEHOLDER 20
166                    // PLACEHOLDER 21
167                    // PLACEHOLDER 22
168    
169                    if (_isOmniAdmin(request)) {
170                            LicenseUtil.registerOrder(request);
171    
172                            return true;
173                    }
174                    else {
175                            return false;
176                    }
177            }
178    
179    }