001    /**
002     * Copyright (c) 2000-2013 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 actionMapping, ActionForm actionForm,
050                            HttpServletRequest request, 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.APPLICATION_JSON);
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.APPLICATION_JSON);
081    
082                                    ServletResponseUtil.write(response, serverInfo);
083    
084                                    return null;
085                            }
086    
087                            return actionMapping.findForward("portal.license");
088                    }
089    
090                    response.sendRedirect(PortalUtil.getPathContext() + "/c/portal/layout");
091    
092                    return null;
093            }
094    
095            private String _getLicenseProperties(String clusterNodeId) {
096                    List<Map<String, String>> licenseProperties =
097                            LicenseManagerUtil.getClusterLicenseProperties(clusterNodeId);
098    
099                    if (licenseProperties == null) {
100                            return StringPool.BLANK;
101                    }
102    
103                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
104    
105                    for (Map<String, String> propertiesMap : licenseProperties) {
106                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
107    
108                            for (Map.Entry<String, String> entry : propertiesMap.entrySet()) {
109                                    jsonObject.put(entry.getKey(), entry.getValue());
110                            }
111    
112                            jsonArray.put(jsonObject);
113                    }
114    
115                    return jsonArray.toString();
116            }
117    
118            private String _getServerInfo(String clusterNodeId) throws Exception {
119                    Map<String, String> serverInfo = LicenseUtil.getClusterServerInfo(
120                            clusterNodeId);
121    
122                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
123    
124                    if (serverInfo != null) {
125                            for (Map.Entry<String, String> entry : serverInfo.entrySet()) {
126                                    jsonObject.put(entry.getKey(), entry.getValue());
127                            }
128                    }
129    
130                    return jsonObject.toString();
131            }
132    
133            private boolean _isOmniAdmin(HttpServletRequest request) {
134                    User user = null;
135    
136                    try {
137                            user = PortalUtil.getUser(request);
138                    }
139                    catch (Exception e) {
140                    }
141    
142                    if ((user != null) && OmniadminUtil.isOmniadmin(user)) {
143                            return true;
144                    }
145                    else {
146                            return false;
147                    }
148            }
149    
150            private boolean _isValidRequest(HttpServletRequest request) {
151    
152                    // PLACEHOLDER 09
153                    // PLACEHOLDER 10
154                    // PLACEHOLDER 11
155                    // PLACEHOLDER 12
156                    // PLACEHOLDER 13
157                    // PLACEHOLDER 14
158                    // PLACEHOLDER 15
159                    // PLACEHOLDER 16
160                    // PLACEHOLDER 17
161                    // PLACEHOLDER 18
162                    // PLACEHOLDER 19
163                    // PLACEHOLDER 20
164                    // PLACEHOLDER 21
165                    // PLACEHOLDER 22
166    
167                    if (_isOmniAdmin(request)) {
168                            LicenseUtil.registerOrder(request);
169    
170                            return true;
171                    }
172                    else {
173                            return false;
174                    }
175            }
176    
177    }