001/*
002 *  Copyright 2020 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.mobileapp.action;
017
018import java.io.IOException;
019import java.io.InputStream;
020import java.nio.charset.StandardCharsets;
021import java.util.Map;
022
023import javax.servlet.http.HttpServletRequest;
024
025import org.apache.avalon.framework.parameters.Parameters;
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028import org.apache.cocoon.acting.ServiceableAction;
029import org.apache.cocoon.environment.ObjectModelHelper;
030import org.apache.cocoon.environment.Redirector;
031import org.apache.cocoon.environment.Request;
032import org.apache.cocoon.environment.SourceResolver;
033import org.apache.cocoon.environment.http.HttpEnvironment;
034import org.apache.cocoon.environment.http.HttpResponse;
035
036import org.ametys.core.cocoon.JSonReader;
037import org.ametys.core.util.JSONUtils;
038
039/**
040 * An abstract action to bet the request parameters (esther in GET or POST)
041 */
042abstract class AbstractPostAction extends ServiceableAction
043{
044    /** JSON Utils */
045    protected JSONUtils _jsonUtils;
046    
047    @Override
048    public void service(ServiceManager smanager) throws ServiceException
049    {
050        _jsonUtils = (JSONUtils) smanager.lookup(JSONUtils.ROLE);
051    }
052    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
053    {
054        Request request = ObjectModelHelper.getRequest(objectModel);
055        Map<String, Object> jsonParams = getParameters(objectModel);
056        
057        Map<String, Object> result = doAction(request, jsonParams);
058        
059        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
060        
061        if (result.get("code") != Integer.valueOf(200))
062        {
063            HttpResponse response = (HttpResponse) ObjectModelHelper.getResponse(objectModel);
064            response.setStatus((int) result.get("code"));
065        }
066        return EMPTY_MAP;
067    }
068    
069    /**
070     * Get a map of parameters from the POST body
071     * @param objectModel the objectModel from whidh the body will be read
072     * @return a Map representing the body, parsed as json
073     * @throws IOException something went wrong
074     */
075    protected Map<String, Object> getParameters(Map objectModel) throws IOException
076    {
077        HttpServletRequest postReq = (HttpServletRequest) objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT);
078
079        InputStream postBody = postReq.getInputStream();
080        String body = new String(postBody.readAllBytes(), StandardCharsets.UTF_8);
081        Map<String, Object> json = _jsonUtils.convertJsonToMap(body);
082        
083        return json;
084    }
085    
086    /**
087     * Get a parameter either from the POST body or directly from the request parameters
088     * @param name the name of the parameter to fetch
089     * @param jsonParams the json parameters passed
090     * @param request the request to read from
091     * @return an Object (if from json) or a String (if from the request)
092     */
093    protected Object getParameter(String name, Map<String, Object> jsonParams, Request request)
094    {
095        if (jsonParams.containsKey(name))
096        {
097            return jsonParams.get(name);
098        }
099        else
100        {
101            return request.getParameter(name);
102        }
103    }
104    
105    /**
106     * Execute the action
107     * @param request the request
108     * @param jsonParams map of parameters passed in the post body as json
109     * @return the json map to return
110     */
111    protected abstract Map<String, Object> doAction(Request request, Map<String, Object> jsonParams);
112}