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.util.HashMap;
019import java.util.Map;
020
021import org.apache.cocoon.environment.Request;
022import org.apache.commons.lang3.StringUtils;
023
024/**
025 * Returns the latest version compatible between client and server
026 */
027public class LatestVersionAction extends AbstractPostAction
028{
029    /** Application current version */
030    protected static final int _CURRENT_VERSION = 1;
031    
032    /** Parameter containing the client version */
033    protected static final String _CLIENT_VERSION = "app_version";
034    
035    @Override
036    protected Map<String, Object> doAction(Request request, Map<String, Object> jsonParams)
037    {
038        Map<String, Object> result = new HashMap<>();
039        String clientVersionString = (String) getParameter(_CLIENT_VERSION, jsonParams, request);
040        
041        int clientVersion = 1;
042        
043        if (StringUtils.isNotBlank(clientVersionString))
044        {
045            clientVersion = Integer.parseInt(clientVersionString);
046        }
047        
048        int communicationVersion = _CURRENT_VERSION;
049        if (clientVersion < _CURRENT_VERSION)
050        {
051            communicationVersion = clientVersion;
052        }
053        
054        result.put("code", 200);
055        result.put("server_version", communicationVersion);
056        
057        return result;
058    }
059
060}