001/*
002 * Stallion Core: A Modern Web Framework
003 *
004 * Copyright (C) 2015 - 2016 Stallion Software LLC.
005 *
006 * This program is free software: you can redistribute it and/or modify it under the terms of the
007 * GNU General Public License as published by the Free Software Foundation, either version 2 of
008 * the License, or (at your option) any later version. This program is distributed in the hope that
009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
011 * License for more details. You should have received a copy of the GNU General Public License
012 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
013 *
014 *
015 *
016 */
017
018package io.stallion.requests.validators;
019
020import io.stallion.exceptions.ClientException;
021
022import java.text.MessageFormat;
023import java.util.Map;
024
025import static io.stallion.utils.Literals.*;
026import static io.stallion.Context.*;
027
028/**
029 * A helper class for extracting required parameters from a user provided
030 * hashmap.
031 *
032 * So instead of doing:
033 * Map params = request().getBodyMap();
034 * String code = params.getOrDefault("code", null);
035 * if (empty(code)) {
036 *     throw new ClientException("Could not find required parameter: code");
037 * }
038 *
039 * String redirectUri = params.getOrDefault("redirectUri", null);
040 * if (empty(code)) {
041 *     throw new ClientException("Could not find required parameter: redirectUri");
042 * }
043 *
044 * You do:
045 *
046 * ParamExtractor&lt;String&gt; params = new ParamExtractor(request().getBodyMap(), "Required post body parameter {0} was not found.");
047 * String code = params.get("code");
048 * String redirectUri = params.get("redirect_uri");
049 *
050 * And the exception with proper messaging automatically gets bubbled up to the user if the parameter is empty.
051 *
052 * @param <T>
053 */
054public class ParamExtractor<T> {
055    private Map<Object, T> map;
056    private String messageTemplate;
057
058    public ParamExtractor(Map<Object, T> map) {
059        this(map, null);
060    }
061
062
063    /**
064     *
065     * @param map
066     * @param messageTemplate - Formatted using MessageFormat use {0} for the parameter name.
067     */
068    public ParamExtractor(Map map, String messageTemplate) {
069        this.map = map;
070        if (empty(messageTemplate)) {
071            messageTemplate = "Could not find required parameter: {0}";
072        }
073        this.messageTemplate = messageTemplate;
074
075    }
076
077    /**
078     * New RequiredParamMapper from the current request body.
079     *
080     * @param <T>
081     * @return
082     */
083    public <T> ParamExtractor<T> fromRequest() {
084        return new ParamExtractor<T>(request().getBodyMap(), "Required POST body parameter {0} was not found.");
085    }
086
087    /**
088     * Gets the parameter, throws a ClientException 400 error with the message template if the parameter
089     * is empty.
090     *
091     * @param key
092     * @return
093     */
094    public T get(Object key) {
095        T value = map.getOrDefault(key, null);
096        if (emptyObject(value)) {
097            throw new ClientException(MessageFormat.format(messageTemplate, key));
098        }
099        return value;
100    }
101
102
103
104}