Java tutorial
package nc.noumea.mairie.appock.ws; /*- * #%L * Logiciel de Gestion des approvisionnements et des stocks des fournitures administratives de la Mairie de Nouma * %% * Copyright (C) 2017 Mairie de Nouma, Nouvelle-Caldonie * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program. If not, see * <http://www.gnu.org/licenses/gpl-3.0.html>. * #L% */ import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.stereotype.Service; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientHandlerException; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; import flexjson.JSONDeserializer; import nc.noumea.mairie.appock.util.LightUserDto; @Service("radiWSConsumer") public class RadiWSConsumer { @Autowired @Qualifier("radiWsBaseUrl") private String radiWsBaseUrl; private static final String searchAgentRadi = "users"; public LightUserDto getLightUserDtoByLoginFromRadi(String login) { String url = String.format(radiWsBaseUrl + searchAgentRadi + "/" + login); ClientResponse res; try { res = createAndFireRequest(new HashMap<>(), url, false, null); return convertResponseAsLightUserDto(res, url); } catch (Exception e) { return null; } } private ClientResponse createAndFireRequest(Map<String, String> parameters, String url, boolean isPost, String postContent) throws Exception { Client client = Client.create(); WebResource webResource = client.resource(url); for (String key : parameters.keySet()) { webResource = webResource.queryParam(key, parameters.get(key)); } ClientResponse response = null; try { if (isPost) if (postContent == null) response = webResource.type(MediaType.APPLICATION_JSON_VALUE).post(ClientResponse.class); else response = webResource.type(MediaType.APPLICATION_JSON_VALUE).post(ClientResponse.class, postContent); else response = webResource.type(MediaType.APPLICATION_JSON_VALUE).get(ClientResponse.class); } catch (ClientHandlerException ex) { throw new Exception(String.format("An error occured when querying '%s'.", url), ex); } return response; } private LightUserDto convertResponseAsLightUserDto(ClientResponse response, String url) throws Exception { if (response.getStatus() == HttpStatus.NO_CONTENT.value()) { return null; } if (response.getStatus() != HttpStatus.OK.value()) { throw new Exception(String.format("An error occured when querying '%s'. Return code is : %s", url, response.getStatus())); } String output = response.getEntity(String.class); HashMap<String, Object> map = (HashMap<String, Object>) new JSONDeserializer().deserialize(output); LightUserDto lightUserDto = new LightUserDto(); lightUserDto.setMail((String) map.get("mail")); lightUserDto.setGivenName((String) map.get("givenName")); lightUserDto.setsAMAccountName((String) map.get("sAMAccountName")); lightUserDto.setSn((String) map.get("sn")); lightUserDto.setTelephoneNumber((String) map.get("telephoneNumber")); return lightUserDto; } }