Search in sources :

Example 1 with MessageParams

use of net.osmand.server.api.services.MotdService.MessageParams in project OsmAnd-tools by osmandapp.

the class ApiController method getMessage.

@GetMapping(path = { "/motd", "/motd.php" })
@ResponseBody
public String getMessage(@RequestParam(required = false) String version, @RequestParam(required = false) Integer nd, @RequestParam(required = false) Integer ns, @RequestParam(required = false) String lang, @RequestParam(required = false) String os, @RequestParam(required = false) String aid, @RequestParam(required = false) String discount, @RequestHeader HttpHeaders headers, HttpServletRequest request) throws IOException, ParseException {
    MessageParams params = new MessageParams();
    params.hostAddress = request.getRemoteAddr();
    if (headers.getFirst("X-Forwarded-For") != null) {
        params.hostAddress = headers.getFirst("X-Forwarded-For");
    }
    params.numberOfDays = nd;
    params.numberOfStarts = ns;
    params.appVersion = "";
    params.os = os;
    params.lang = lang;
    if (version != null) {
        int i = version.indexOf(" ");
        if (i >= 0) {
            params.appVersion = version.substring(0, i);
            params.version = version.substring(i + 1);
        } else {
            params.version = version;
        }
    }
    HashMap<String, Object> body = motdService.getMessage(params);
    if (body != null) {
        return jsonMapper.writeValueAsString(body);
    }
    return "{}";
}
Also used : MessageParams(net.osmand.server.api.services.MotdService.MessageParams) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with MessageParams

use of net.osmand.server.api.services.MotdService.MessageParams in project OsmAnd-tools by osmandapp.

the class ApiController method getActiveSubscriptions.

@GetMapping(path = { "/subscriptions/active" })
@ResponseBody
public FileSystemResource getActiveSubscriptions(@RequestParam(required = false) String version, @RequestParam(required = false) String androidPackage, @RequestParam(required = false) Integer nd, @RequestParam(required = false) Integer ns, @RequestParam(required = false) String lang, @RequestParam(required = false) String os, @RequestParam(required = false) String aid, @RequestHeader HttpHeaders headers, HttpServletRequest request) throws IOException, ParseException {
    MessageParams params = new MessageParams();
    params.hostAddress = request.getRemoteAddr();
    if (headers.getFirst("X-Forwarded-For") != null) {
        params.hostAddress = headers.getFirst("X-Forwarded-For");
    }
    params.os = os;
    params.lang = lang;
    params.numberOfDays = nd;
    params.numberOfStarts = ns;
    params.appVersion = "";
    params.appPackage = androidPackage;
    if (version != null) {
        int i = version.indexOf(" ");
        if (i >= 0) {
            params.appVersion = version.substring(0, i);
            params.version = version.substring(i + 1);
        } else {
            params.version = version;
        }
    }
    File file = motdService.getSubscriptions(params);
    FileSystemResource fsr = new FileSystemResource(file);
    return fsr;
}
Also used : MessageParams(net.osmand.server.api.services.MotdService.MessageParams) FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with MessageParams

use of net.osmand.server.api.services.MotdService.MessageParams in project OsmAnd-tools by osmandapp.

the class ApiController method getSubscriptions.

@GetMapping(path = { "/subscriptions/get" })
@ResponseBody
public String getSubscriptions(@RequestParam(required = false) String userId, @RequestParam(required = false) String userToken, @RequestParam(required = false) String orderId, @RequestHeader HttpHeaders headers, HttpServletRequest request) throws IOException, ParseException {
    MessageParams params = new MessageParams();
    params.hostAddress = request.getRemoteAddr();
    if (headers.getFirst("X-Forwarded-For") != null) {
        params.hostAddress = headers.getFirst("X-Forwarded-For");
    }
    if (!Algorithms.isEmpty(userId) && !Algorithms.isEmpty(userToken) && Algorithms.isEmpty(orderId)) {
        Optional<Supporter> sup = supportersRepository.findByUserId(Long.parseLong(userId));
        if (sup.isPresent()) {
            Supporter s = sup.get();
            if (userToken.equals(s.token)) {
                orderId = sup.get().orderId;
            }
        }
    }
    if (!Algorithms.isEmpty(orderId)) {
        List<SupporterDeviceSubscription> subscriptions = subscriptionsRepository.findByOrderId(orderId);
        List<Object> res = new ArrayList<>();
        for (SupporterDeviceSubscription sub : subscriptions) {
            Map<String, String> subMap = new HashMap<>();
            subMap.put("sku", sub.sku);
            if (sub.valid != null) {
                subMap.put("valid", sub.valid.toString());
            }
            String state = "undefined";
            if (sub.starttime != null) {
                subMap.put("start_time", String.valueOf(sub.starttime.getTime()));
            }
            if (sub.expiretime != null) {
                long expireTimeMs = sub.expiretime.getTime();
                int paymentState = sub.paymentstate == null ? 1 : sub.paymentstate.intValue();
                boolean autoRenewing = sub.autorenewing == null ? false : sub.autorenewing.booleanValue();
                if (expireTimeMs > System.currentTimeMillis()) {
                    if (paymentState == 1 && autoRenewing) {
                        state = "active";
                    } else if (paymentState == 1 && !autoRenewing) {
                        state = "cancelled";
                    } else if (paymentState == 0 && autoRenewing) {
                        state = "in_grace_period";
                    }
                } else {
                    if (paymentState == 0 && autoRenewing) {
                        state = "on_hold";
                    } else if (paymentState == 1 && autoRenewing) {
                        state = "paused";
                    } else if (paymentState == 1 && !autoRenewing) {
                        state = "expired";
                    }
                }
                subMap.put("expire_time", String.valueOf(expireTimeMs));
            }
            subMap.put("state", state);
            res.add(subMap);
        }
        return jsonMapper.writeValueAsString(res);
    }
    return "{}";
}
Also used : HashMap(java.util.HashMap) MessageParams(net.osmand.server.api.services.MotdService.MessageParams) ArrayList(java.util.ArrayList) SupporterDeviceSubscription(net.osmand.server.api.repo.DeviceSubscriptionsRepository.SupporterDeviceSubscription) Supporter(net.osmand.server.api.repo.SupportersRepository.Supporter) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

MessageParams (net.osmand.server.api.services.MotdService.MessageParams)3 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 SupporterDeviceSubscription (net.osmand.server.api.repo.DeviceSubscriptionsRepository.SupporterDeviceSubscription)1 Supporter (net.osmand.server.api.repo.SupportersRepository.Supporter)1 FileSystemResource (org.springframework.core.io.FileSystemResource)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1