Java tutorial
package ch.cyberduck.core.hubic; /* * Copyright (c) 2002-2016 iterate GmbH. All rights reserved. * https://cyberduck.io/ * * 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 2 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. */ import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ResponseHandler; import org.apache.http.entity.ContentType; import org.apache.http.protocol.HTTP; import java.io.IOException; import java.io.InputStreamReader; import java.net.URI; import java.nio.charset.Charset; import java.util.Collections; import ch.iterate.openstack.swift.AuthenticationResponse; import ch.iterate.openstack.swift.Response; import ch.iterate.openstack.swift.exception.AuthorizationException; import ch.iterate.openstack.swift.exception.GenericException; import ch.iterate.openstack.swift.model.Region; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import com.google.gson.JsonParser; public class HubicAuthenticationResponseHandler implements ResponseHandler<AuthenticationResponse> { @Override public AuthenticationResponse handleResponse(final HttpResponse response) throws IOException { if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { Charset charset = HTTP.DEF_CONTENT_CHARSET; ContentType contentType = ContentType.get(response.getEntity()); if (contentType != null) { if (contentType.getCharset() != null) { charset = contentType.getCharset(); } } try { final JsonParser parser = new JsonParser(); final JsonObject json = parser .parse(new InputStreamReader(response.getEntity().getContent(), charset)).getAsJsonObject(); final String token = json.getAsJsonPrimitive("token").getAsString(); final String endpoint = json.getAsJsonPrimitive("endpoint").getAsString(); return new AuthenticationResponse(response, token, Collections.singleton(new Region(null, URI.create(endpoint), null, true))); } catch (JsonParseException e) { throw new IOException(e.getMessage(), e); } } else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED || response.getStatusLine().getStatusCode() == HttpStatus.SC_FORBIDDEN) { throw new AuthorizationException(new Response(response)); } throw new GenericException(new Response(response)); } }