fr.mailjet.rest.impl.UserRESTServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for fr.mailjet.rest.impl.UserRESTServiceImpl.java

Source

package fr.mailjet.rest.impl;

/*
 *  Mailjet %% Copyright (C) 2012 Pitton Olivier - olivier dot pitton at
 * gmail dot com %% Licensed under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License. #L%
 */

import java.util.Map;

import javax.ws.rs.core.MultivaluedMap;

import org.apache.commons.lang3.StringUtils;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.UniformInterfaceException;

import fr.mailjet.context.MailjetContext;
import fr.mailjet.rest.UserRESTService;
import fr.mailjet.rest.parameters.EnumReturnType;

/**
 * Une implmentation par dfaut du service <i>User</i>
 * 
 * @author Pitton Olivier
 * 
 */
public class UserRESTServiceImpl extends AbstractRESTService implements UserRESTService {

    /**
     * Cl du paramtre "domain"
     * 
     * @see #domainAdd(EnumReturnType, String)
     */
    static final private String _domainAdd = "domain";

    /**
     * Cl du paramtre "email"
     * 
     * @see #senderAdd(EnumReturnType, String)
     * @see #senderStatus(EnumReturnType, String)
     */
    static final private String _email = "email";

    /**
     * Cl du paramtre "click"
     * 
     * @see #trackingUpdate(EnumReturnType, Boolean, Boolean)
     */
    static final private String _click = "click";

    /**
     * Cl du paramtre "open"
     * 
     * @see #trackingUpdate(EnumReturnType, Boolean, Boolean)
     */

    static final private String _open = "open";

    /**
     * Constructeur
     * 
     * @param parContext
     *          {@link MailjetContext} un contexte
     * @param parClient
     *          {@link Client} un client HTTP
     */
    protected UserRESTServiceImpl(MailjetContext parContext, Client parClient) {
        super(parContext, parClient);
    }

    @Override
    public String infos(EnumReturnType parReturnType) throws UniformInterfaceException {
        return this.createGETRequest(parReturnType, "userInfos");
    }

    @Override
    public String domainList(EnumReturnType parReturnType) throws UniformInterfaceException {
        return this.createGETRequest(parReturnType, "userDomainlist");
    }

    @Override
    public String senderList(EnumReturnType parReturnType) throws UniformInterfaceException {
        return this.createGETRequest(parReturnType, "userSenderlist");
    }

    @Override
    public String trackingCheck(EnumReturnType parReturnType) throws UniformInterfaceException {
        return this.createGETRequest(parReturnType, "userTrackingcheck");
    }

    @Override
    public String domainAdd(EnumReturnType parType, String parDomainName)
            throws UniformInterfaceException, IllegalArgumentException {
        if (StringUtils.isEmpty(parDomainName))
            throw new IllegalArgumentException();
        return this.createPOSTRequest(parType, "userDomainadd", _domainAdd, parDomainName);
    }

    @Override
    public String domainStatus(EnumReturnType parType, String parDomainName)
            throws UniformInterfaceException, IllegalArgumentException {
        return this.domainStatus(parType, parDomainName, null);
    }

    @Override
    public String domainStatus(EnumReturnType parType, String parDomainName, Boolean parCheck)
            throws UniformInterfaceException, IllegalArgumentException {
        MultivaluedMap<String, String> locParameters = this.createHTTPProperties(parType);
        if (StringUtils.isEmpty(parDomainName))
            throw new IllegalArgumentException();
        locParameters.putSingle(_domainAdd, parDomainName);
        if (parCheck != null) {
            int locRealValue = parCheck.booleanValue() ? 1 : 0;
            locParameters.putSingle("check", Integer.valueOf(locRealValue).toString());
        }
        return this.createPOSTRequest("userDomainstatus", locParameters);
    }

    @Override
    public String senderAdd(EnumReturnType parType, String parEmail)
            throws UniformInterfaceException, IllegalArgumentException {
        return this.createPOSTRequest(parType, "userSenderadd", _email, parEmail);
    }

    @Override
    public String senderStatus(EnumReturnType parType, String parEmail)
            throws UniformInterfaceException, IllegalArgumentException {
        return this.createPOSTRequest(parType, "userSenderstatus", _email, parEmail);
    }

    @Override
    public String trackingUpdate(EnumReturnType parType, Boolean parOpen, Boolean parClick)
            throws UniformInterfaceException, IllegalArgumentException {
        if (parOpen == null || parClick == null)
            throw new IllegalArgumentException();

        MultivaluedMap<String, String> locParameters = this.createHTTPProperties(parType);
        Integer locClick = this.toInteger(parClick);
        locParameters.putSingle(_click, locClick.toString());
        Integer locOpen = this.toInteger(parOpen);
        locParameters.putSingle(_open, locOpen.toString());
        return this.createPOSTRequest("userTrackingupdate", locParameters);
    }

    @Override
    public String update(EnumReturnType parType, Map<String, String> parParameters)
            throws UniformInterfaceException {
        MultivaluedMap<String, String> locParameters = this.createHTTPProperties(parType);
        if (parParameters != null) {
            for (Map.Entry<String, String> locEntry : parParameters.entrySet()) {
                locParameters.putSingle(locEntry.getKey(), locEntry.getValue());
            }
        }
        return this.createPOSTRequest("userUpdate", locParameters);
    }
}