Java tutorial
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.List; 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.ApiRESTService; import fr.mailjet.rest.parameters.EnumCustomStatus; import fr.mailjet.rest.parameters.EnumReturnType; /** * Une implmentation par dfaut du service <i>API</i> * * @author Pitton Olivier * */ public class ApiRESTServiceImpl extends AbstractRESTService implements ApiRESTService { /** * Cl du paramtre "api_key" des requtes keySecret et keySecretChange<br /> * <a href="http://fr.mailjet.com/docs/api/api/keylist">Documentation * Mailjet</a> */ static final private String _apiKeyProperty = "apikey"; /** * Cl du paramtre "active" de la requte keyList.<br /> * <a href="http://fr.mailjet.com/docs/api/api/keylist">Documentation * Mailjet</a> */ static final private String _activeProperty = "active"; /** * Cl du paramtre "custom_status" de la requte keyList.<br /> * <a href="http://fr.mailjet.com/docs/api/api/keylist">Documentation * Mailjet</a> */ static final private String _customStatusProperty = "custom_status"; /** * Cl du paramtre "name" de la requte keyList.<br /> * <a href="http://fr.mailjet.com/docs/api/api/keylist">Documentation * Mailjet</a> */ static final private String _nameProperty = "name"; /** * Cl du paramtre "type" de la requte keyList.<br /> * <a href="http://fr.mailjet.com/docs/api/api/keylist">Documentation * Mailjet</a> */ static final private String _typeProperty = "type"; /** * Cl du paramtre "allowed_access" de la requte keyAuthenticate.<br /> * <a href="https://fr.mailjet.com/docs/api/api/keyauthenticate">Documentation * Mailjet</a> */ static final private String _defaultAccessProperty = "allowed_access"; /** * Constructeur * * @param parContext * {@link MailjetContext} un contexte * @param parClient * {@link Client} un client HTTP */ protected ApiRESTServiceImpl(MailjetContext parContext, Client parClient) { super(parContext, parClient); } @Override public String keyList(EnumReturnType parType, Boolean parIsActive) throws UniformInterfaceException { return this.keyList(parType, parIsActive, null); } @Override public String keyList(EnumReturnType parType, Boolean parIsActive, EnumCustomStatus parStatus) throws UniformInterfaceException { return this.keyList(parType, parIsActive, parStatus, null); } @Override public String keyList(EnumReturnType parType, Boolean parIsActive, EnumCustomStatus parStatus, String parName) throws UniformInterfaceException { return this.keyList(parType, parIsActive, parStatus, parName, null); } @Override public String keyList(EnumReturnType parType, Boolean parIsActive, EnumCustomStatus parStatus, String parName, Boolean parUserType) throws UniformInterfaceException { MultivaluedMap<String, String> locProperties = this.createHTTPProperties(parType); // On ajoute l'ensemble des proprits non null // active property if (parIsActive != null) { // 1 : active - 0 : inactive int locActiveValue = parIsActive.booleanValue() ? 1 : 0; locProperties.putSingle(_activeProperty, Integer.toString(locActiveValue)); } // custom property if (parStatus != null) { locProperties.putSingle(_customStatusProperty, parStatus.getConstName()); } // name property if (StringUtils.isNotEmpty(parName)) { locProperties.putSingle(_nameProperty, parName); } // type property if (parUserType != null) { // 1 : main / 0 : subuser int locTypeValue = parUserType.booleanValue() ? 1 : 0; locProperties.putSingle(_typeProperty, Integer.toString(locTypeValue)); } return this.createGETRequest("apiKeylist", locProperties); } @Override public String keySecret(EnumReturnType parType) throws UniformInterfaceException { MultivaluedMap<String, String> locMap = this.createHTTPProperties(parType); locMap.putSingle(_apiKeyProperty, this._context.getApiKey()); return this.createGETRequest("apiKeysecret", locMap); } @Override public String keySecretChange(EnumReturnType parType) throws UniformInterfaceException { MultivaluedMap<String, String> locMap = this.createHTTPProperties(parType); locMap.putSingle(_apiKeyProperty, this._context.getApiKey()); return this.createGETRequest("apiKeysecretchange", locMap); } @Override public String keyAdd(EnumReturnType parType, String parCustomName) throws UniformInterfaceException, IllegalArgumentException { return this.keyAdd(parType, parCustomName, null); } @Override public String keyAdd(EnumReturnType parType, String parCustomName, EnumCustomStatus parStatus) throws UniformInterfaceException, IllegalArgumentException { if (StringUtils.isEmpty(parCustomName)) throw new IllegalArgumentException(); MultivaluedMap<String, String> locParameters = this.createHTTPProperties(parType); locParameters.putSingle(_nameProperty, parCustomName); if (parStatus != null) { locParameters.putSingle(_customStatusProperty, parStatus.getConstName()); } return this.createPOSTRequest("apiKeyAdd", locParameters); } @Override public String keyAuthenticate(EnumReturnType parType, List<String> parAllowedAccess) throws UniformInterfaceException, IllegalArgumentException { return this.keyAuthenticate(parType, parAllowedAccess, null); } @Override public String keyAuthenticate(EnumReturnType parType, List<String> parAllowedAccess, Map<String, String> parParameters) throws UniformInterfaceException, IllegalArgumentException { MultivaluedMap<String, String> locMap = this.createHTTPProperties(parType); locMap.putSingle(_apiKeyProperty, this.getContext().getApiKey()); if (parAllowedAccess == null || parAllowedAccess.isEmpty()) throw new IllegalArgumentException(); locMap.put(_defaultAccessProperty, parAllowedAccess); if (parParameters != null) { for (Map.Entry<String, String> locEntry : parParameters.entrySet()) { locMap.putSingle(locEntry.getKey(), locEntry.getValue()); } } return this.createPOSTRequest("apiKeyauthenticate", locMap); } @Override public String keyUpdate(EnumReturnType parType) throws UniformInterfaceException { return this.keyUpdate(parType, this.getContext().getApiKey()); } @Override public String keyUpdate(EnumReturnType parType, String parApiKey) throws UniformInterfaceException, IllegalArgumentException { return this.keyUpdate(parType, parApiKey, null); } @Override public String keyUpdate(EnumReturnType parType, String parApiKey, EnumCustomStatus parStatus) throws UniformInterfaceException, IllegalArgumentException { return this.keyUpdate(parType, parApiKey, parStatus, null); } @Override public String keyUpdate(EnumReturnType parType, String parApiKey, EnumCustomStatus parStatus, String parName) throws UniformInterfaceException, IllegalArgumentException { MultivaluedMap<String, String> locMap = this.createHTTPProperties(parType); if (StringUtils.isEmpty(parApiKey)) throw new IllegalArgumentException(); locMap.putSingle(_apiKeyProperty, parApiKey); if (parStatus != null) { locMap.putSingle(_customStatusProperty, parStatus.getConstName()); } if (StringUtils.isNotEmpty(parName)) { locMap.putSingle(_nameProperty, parName); } return this.createPOSTRequest("apiKeyupdate", locMap); } }