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                    if (_isValidRequest(request)) {
054                            String cmd = ParamUtil.getString(request, Constants.CMD);
055    
056                            String clusterNodeId = ParamUtil.getString(
057                                    request, "clusterNodeId");
058    
059                            if (cmd.equals("licenseProperties")) {
060                                    String licenseProperties = _getLicenseProperties(clusterNodeId);
061    
062                                    response.setContentType(ContentTypes.APPLICATION_JSON);
063    
064                                    ServletResponseUtil.write(response, licenseProperties);
065    
066                                    return null;
067                            }
068                            else if (cmd.equals("serverInfo")) {
069                                    String serverInfo = _getServerInfo(clusterNodeId);
070    
071                                    response.setContentType(ContentTypes.APPLICATION_JSON);
072    
073                                    ServletResponseUtil.write(response, serverInfo);
074    
075                                    return null;
076                            }
077    
078                            return actionMapping.findForward("portal.license");
079                    }
080                    else {
081                            response.sendRedirect(
082                                    PortalUtil.getPathContext() + "/c/portal/layout");
083    
084                            return null;
085                    }
086            }
087    
088            private String _getLicenseProperties(String clusterNodeId) {
089                    List<Map<String, String>> licenseProperties =
090                            LicenseManagerUtil.getClusterLicenseProperties(clusterNodeId);
091    
092                    if (licenseProperties == null) {
093                            return StringPool.BLANK;
094                    }
095    
096                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
097    
098                    for (Map<String, String> propertiesMap : licenseProperties) {
099                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
100    
101                            for (Map.Entry<String, String> entry : propertiesMap.entrySet()) {
102                                    jsonObject.put(entry.getKey(), entry.getValue());
103                            }
104    
105                            jsonArray.put(jsonObject);
106                    }
107    
108                    return jsonArray.toString();
109            }
110    
111            private String _getServerInfo(String clusterNodeId) throws Exception {
112                    Map<String, String> serverInfo = LicenseUtil.getClusterServerInfo(
113                            clusterNodeId);
114    
115                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
116    
117                    if (serverInfo != null) {
118                            for (Map.Entry<String, String> entry : serverInfo.entrySet()) {
119                                    jsonObject.put(entry.getKey(), entry.getValue());
120                            }
121                    }
122    
123                    return jsonObject.toString();
124            }
125    
126            private boolean _isOmniAdmin(HttpServletRequest request) {
127                    User user = null;
128    
129                    try {
130                            user = PortalUtil.getUser(request);
131                    }
132                    catch (Exception e) {
133                    }
134    
135                    if ((user != null) && OmniadminUtil.isOmniadmin(user.getUserId())) {
136                            return true;
137                    }
138                    else {
139                            return false;
140                    }
141            }
142    
143            private boolean _isValidRequest(HttpServletRequest request) {
144                    if (_isOmniAdmin(request)) {
145                            LicenseUtil.registerOrder(request);
146    
147                            return true;
148                    }
149                    else {
150                            return false;
151                    }
152            }
153    
154    }