Merge pull request #1385 from DaanHoogland/CLOUDSTACK-9265

CLOUDSTACK-9265 cleanup around httpclient versionssome cleanup done
- replaced HttpStatus from org.apache.commons.httpclient with that from org.apache.http
- removed unthrown HttpException
- left auto reformat in place

* pr/1385:
  CLOUDSTACK-9265 cleanup around httpclient versions

Signed-off-by: Will Stevens <williamstevens@gmail.com>
This commit is contained in:
Will Stevens 2016-05-12 11:11:27 -04:00
commit 688522ecd4
5 changed files with 103 additions and 105 deletions

View File

@ -24,11 +24,9 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import junit.framework.Assert;
import org.apache.commons.httpclient.HttpException;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead; import org.apache.http.client.methods.HttpHead;
import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.DefaultHttpClient;
@ -37,13 +35,15 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Parameters; import org.testng.annotations.Parameters;
import junit.framework.Assert;
@ContextConfiguration(locations = "classpath:/storageContext.xml") @ContextConfiguration(locations = "classpath:/storageContext.xml")
public class TestHttp extends AbstractTestNGSpringContextTests { public class TestHttp extends AbstractTestNGSpringContextTests {
@Test @Test
@Parameters("template-url") @Parameters("template-url")
public void testHttpclient(String templateUrl) { public void testHttpclient(String templateUrl) {
HttpHead method = new HttpHead(templateUrl); final HttpHead method = new HttpHead(templateUrl);
DefaultHttpClient client = new DefaultHttpClient(); final DefaultHttpClient client = new DefaultHttpClient();
OutputStream output = null; OutputStream output = null;
long length = 0; long length = 0;
@ -51,34 +51,35 @@ public class TestHttp extends AbstractTestNGSpringContextTests {
HttpResponse response = client.execute(method); HttpResponse response = client.execute(method);
length = Long.parseLong(response.getFirstHeader("Content-Length").getValue()); length = Long.parseLong(response.getFirstHeader("Content-Length").getValue());
System.out.println(response.getFirstHeader("Content-Length").getValue()); System.out.println(response.getFirstHeader("Content-Length").getValue());
File localFile = new File("/tmp/test"); final File localFile = new File("/tmp/test");
if (!localFile.exists()) { if (!localFile.exists()) {
localFile.createNewFile(); localFile.createNewFile();
} }
HttpGet getMethod = new HttpGet(templateUrl); final HttpGet getMethod = new HttpGet(templateUrl);
response = client.execute(getMethod); response = client.execute(getMethod);
HttpEntity entity = response.getEntity(); final HttpEntity entity = response.getEntity();
output = new BufferedOutputStream(new FileOutputStream(localFile)); output = new BufferedOutputStream(new FileOutputStream(localFile));
entity.writeTo(output); entity.writeTo(output);
} catch (HttpException e) { } catch (final ClientProtocolException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} catch (IOException e) { } catch (final IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
try { try {
if (output != null) if (output != null) {
output.close(); output.close();
} catch (IOException e) { }
} catch (final IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }
File f = new File("/tmp/test"); final File f = new File("/tmp/test");
Assert.assertEquals(f.length(), length); Assert.assertEquals(f.length(), length);
} }
} }

View File

@ -20,11 +20,11 @@ import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.net.URLDecoder; import java.net.URLDecoder;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.HttpEntityEnclosingRequest; import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpException; import org.apache.http.HttpException;
import org.apache.http.HttpRequest; import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.entity.BasicHttpEntity; import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler; import org.apache.http.protocol.HttpRequestHandler;
@ -55,14 +55,14 @@ public class ClusterServiceServletHttpHandler implements HttpRequestHandler {
s_logger.trace("Handle cluster HTTP request done"); s_logger.trace("Handle cluster HTTP request done");
} }
} catch (Throwable e) { } catch (final Throwable e) {
if (s_logger.isDebugEnabled()) { if (s_logger.isDebugEnabled()) {
s_logger.debug("Exception " + e.toString()); s_logger.debug("Exception " + e.toString());
} }
try { try {
writeResponse(response, HttpStatus.SC_INTERNAL_SERVER_ERROR, null); writeResponse(response, HttpStatus.SC_INTERNAL_SERVER_ERROR, null);
} catch (Throwable e2) { } catch (final Throwable e2) {
if (s_logger.isDebugEnabled()) { if (s_logger.isDebugEnabled()) {
s_logger.debug("Exception " + e2.toString()); s_logger.debug("Exception " + e2.toString());
} }
@ -73,20 +73,20 @@ public class ClusterServiceServletHttpHandler implements HttpRequestHandler {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
private void parseRequest(HttpRequest request) throws IOException { private void parseRequest(HttpRequest request) throws IOException {
if (request instanceof HttpEntityEnclosingRequest) { if (request instanceof HttpEntityEnclosingRequest) {
HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest)request; final HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest)request;
String body = EntityUtils.toString(entityRequest.getEntity()); final String body = EntityUtils.toString(entityRequest.getEntity());
if (body != null) { if (body != null) {
String[] paramArray = body.split("&"); final String[] paramArray = body.split("&");
if (paramArray != null) { if (paramArray != null) {
for (String paramEntry : paramArray) { for (final String paramEntry : paramArray) {
String[] paramValue = paramEntry.split("="); final String[] paramValue = paramEntry.split("=");
if (paramValue.length != 2) { if (paramValue.length != 2) {
continue; continue;
} }
String name = URLDecoder.decode(paramValue[0]); final String name = URLDecoder.decode(paramValue[0]);
String value = URLDecoder.decode(paramValue[1]); final String value = URLDecoder.decode(paramValue[1]);
if (s_logger.isTraceEnabled()) { if (s_logger.isTraceEnabled()) {
s_logger.trace("Parsed request parameter " + name + "=" + value); s_logger.trace("Parsed request parameter " + name + "=" + value);
@ -103,17 +103,17 @@ public class ClusterServiceServletHttpHandler implements HttpRequestHandler {
content = ""; content = "";
} }
response.setStatusCode(statusCode); response.setStatusCode(statusCode);
BasicHttpEntity body = new BasicHttpEntity(); final BasicHttpEntity body = new BasicHttpEntity();
body.setContentType("text/html; charset=UTF-8"); body.setContentType("text/html; charset=UTF-8");
byte[] bodyData = content.getBytes(); final byte[] bodyData = content.getBytes();
body.setContent(new ByteArrayInputStream(bodyData)); body.setContent(new ByteArrayInputStream(bodyData));
body.setContentLength(bodyData.length); body.setContentLength(bodyData.length);
response.setEntity(body); response.setEntity(body);
} }
protected void handleRequest(HttpRequest req, HttpResponse response) { protected void handleRequest(HttpRequest req, HttpResponse response) {
String method = (String)req.getParams().getParameter("method"); final String method = (String)req.getParams().getParameter("method");
int nMethod = RemoteMethodConstants.METHOD_UNKNOWN; int nMethod = RemoteMethodConstants.METHOD_UNKNOWN;
String responseContent = null; String responseContent = null;
@ -133,22 +133,24 @@ public class ClusterServiceServletHttpHandler implements HttpRequestHandler {
case RemoteMethodConstants.METHOD_UNKNOWN: case RemoteMethodConstants.METHOD_UNKNOWN:
default: default:
assert (false); assert false;
s_logger.error("unrecognized method " + nMethod); s_logger.error("unrecognized method " + nMethod);
break; break;
} }
} catch (Throwable e) { } catch (final Throwable e) {
s_logger.error("Unexpected exception when processing cluster service request : ", e); s_logger.error("Unexpected exception when processing cluster service request : ", e);
} }
if (responseContent != null) { if (responseContent != null) {
if (s_logger.isTraceEnabled()) if (s_logger.isTraceEnabled()) {
s_logger.trace("Write reponse with HTTP OK " + responseContent); s_logger.trace("Write reponse with HTTP OK " + responseContent);
}
writeResponse(response, HttpStatus.SC_OK, responseContent); writeResponse(response, HttpStatus.SC_OK, responseContent);
} else { } else {
if (s_logger.isTraceEnabled()) if (s_logger.isTraceEnabled()) {
s_logger.trace("Write reponse with HTTP Bad request"); s_logger.trace("Write reponse with HTTP Bad request");
}
writeResponse(response, HttpStatus.SC_BAD_REQUEST, null); writeResponse(response, HttpStatus.SC_BAD_REQUEST, null);
} }
@ -156,16 +158,16 @@ public class ClusterServiceServletHttpHandler implements HttpRequestHandler {
private String handleDeliverPduMethodCall(HttpRequest req) { private String handleDeliverPduMethodCall(HttpRequest req) {
String pduSeq = (String)req.getParams().getParameter("pduSeq"); final String pduSeq = (String)req.getParams().getParameter("pduSeq");
String pduAckSeq = (String)req.getParams().getParameter("pduAckSeq"); final String pduAckSeq = (String)req.getParams().getParameter("pduAckSeq");
String sourcePeer = (String)req.getParams().getParameter("sourcePeer"); final String sourcePeer = (String)req.getParams().getParameter("sourcePeer");
String destPeer = (String)req.getParams().getParameter("destPeer"); final String destPeer = (String)req.getParams().getParameter("destPeer");
String agentId = (String)req.getParams().getParameter("agentId"); final String agentId = (String)req.getParams().getParameter("agentId");
String gsonPackage = (String)req.getParams().getParameter("gsonPackage"); final String gsonPackage = (String)req.getParams().getParameter("gsonPackage");
String stopOnError = (String)req.getParams().getParameter("stopOnError"); final String stopOnError = (String)req.getParams().getParameter("stopOnError");
String pduType = (String)req.getParams().getParameter("pduType"); final String pduType = (String)req.getParams().getParameter("pduType");
ClusterServicePdu pdu = new ClusterServicePdu(); final ClusterServicePdu pdu = new ClusterServicePdu();
pdu.setSourcePeer(sourcePeer); pdu.setSourcePeer(sourcePeer);
pdu.setDestPeer(destPeer); pdu.setDestPeer(destPeer);
pdu.setAgentId(Long.parseLong(agentId)); pdu.setAgentId(Long.parseLong(agentId));
@ -180,7 +182,7 @@ public class ClusterServiceServletHttpHandler implements HttpRequestHandler {
} }
private String handlePingMethodCall(HttpRequest req) { private String handlePingMethodCall(HttpRequest req) {
String callingPeer = (String)req.getParams().getParameter("callingPeer"); final String callingPeer = (String)req.getParams().getParameter("callingPeer");
if (s_logger.isDebugEnabled()) { if (s_logger.isDebugEnabled()) {
s_logger.debug("Handle ping request from " + callingPeer); s_logger.debug("Handle ping request from " + callingPeer);

View File

@ -30,9 +30,8 @@ import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller; import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller; import javax.xml.bind.Unmarshaller;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpDelete;
@ -64,9 +63,9 @@ import com.cloud.network.schema.showvcs.Output;
public class BrocadeVcsApi { public class BrocadeVcsApi {
private static final Logger s_logger = Logger.getLogger(BrocadeVcsApi.class); private static final Logger s_logger = Logger.getLogger(BrocadeVcsApi.class);
private String _host; private final String _host;
private String _adminuser; private final String _adminuser;
private String _adminpass; private final String _adminpass;
protected DefaultHttpClient _client; protected DefaultHttpClient _client;
@ -74,7 +73,7 @@ public class BrocadeVcsApi {
String url; String url;
try { try {
url = new URL(Constants.PROTOCOL, _host, Constants.PORT, uri).toString(); url = new URL(Constants.PROTOCOL, _host, Constants.PORT, uri).toString();
} catch (MalformedURLException e) { } catch (final MalformedURLException e) {
s_logger.error("Unable to build Brocade Switch API URL", e); s_logger.error("Unable to build Brocade Switch API URL", e);
throw new BrocadeVcsApiException("Unable to build Brocade Switch API URL", e); throw new BrocadeVcsApiException("Unable to build Brocade Switch API URL", e);
} }
@ -116,7 +115,7 @@ public class BrocadeVcsApi {
if (createInterfaceVlan(vlanId)) { if (createInterfaceVlan(vlanId)) {
PortProfile portProfile = createPortProfile(vlanId, networkId); final PortProfile portProfile = createPortProfile(vlanId, networkId);
if (portProfile != null) { if (portProfile != null) {
return activatePortProfile(portProfile); return activatePortProfile(portProfile);
@ -129,9 +128,9 @@ public class BrocadeVcsApi {
* Activates a port-profile. * Activates a port-profile.
*/ */
private boolean activatePortProfile(PortProfile portProfile) throws BrocadeVcsApiException { private boolean activatePortProfile(PortProfile portProfile) throws BrocadeVcsApiException {
PortProfileGlobal portProfileGlobal = new PortProfileGlobal(); final PortProfileGlobal portProfileGlobal = new PortProfileGlobal();
portProfile.setVlanProfile(null); portProfile.setVlanProfile(null);
Activate activate = new Activate(); final Activate activate = new Activate();
portProfile.setActivate(activate); portProfile.setActivate(activate);
portProfileGlobal.setPortProfile(portProfile); portProfileGlobal.setPortProfile(portProfile);
@ -144,7 +143,7 @@ public class BrocadeVcsApi {
*/ */
private PortProfile createPortProfile(int vlanId, long networkId) throws BrocadeVcsApiException { private PortProfile createPortProfile(int vlanId, long networkId) throws BrocadeVcsApiException {
PortProfile portProfile = new PortProfile(); final PortProfile portProfile = new PortProfile();
portProfile.setName(Constants.PORT_PROFILE_NAME_PREFIX + networkId); portProfile.setName(Constants.PORT_PROFILE_NAME_PREFIX + networkId);
if (executeCreateObject(portProfile, Constants.URI)) { if (executeCreateObject(portProfile, Constants.URI)) {
if (createVlanSubProfile(vlanId, portProfile)) { if (createVlanSubProfile(vlanId, portProfile)) {
@ -158,7 +157,7 @@ public class BrocadeVcsApi {
* Create vlan sub-profile for port-profile * Create vlan sub-profile for port-profile
*/ */
private boolean createVlanSubProfile(int vlanId, PortProfile portProfile) throws BrocadeVcsApiException { private boolean createVlanSubProfile(int vlanId, PortProfile portProfile) throws BrocadeVcsApiException {
VlanProfile vlanProfile = new VlanProfile(); final VlanProfile vlanProfile = new VlanProfile();
portProfile.setVlanProfile(vlanProfile); portProfile.setVlanProfile(vlanProfile);
if (executeUpdateObject(portProfile, Constants.URI)) { if (executeUpdateObject(portProfile, Constants.URI)) {
return configureVlanSubProfile(vlanId, portProfile); return configureVlanSubProfile(vlanId, portProfile);
@ -173,15 +172,15 @@ public class BrocadeVcsApi {
* - configure allowed VLANs for vlan sub-profile * - configure allowed VLANs for vlan sub-profile
*/ */
private boolean configureVlanSubProfile(int vlanId, PortProfile portProfile) throws BrocadeVcsApiException { private boolean configureVlanSubProfile(int vlanId, PortProfile portProfile) throws BrocadeVcsApiException {
SwitchportBasic switchPortBasic = new SwitchportBasic(); final SwitchportBasic switchPortBasic = new SwitchportBasic();
Basic basic = new Basic(); final Basic basic = new Basic();
switchPortBasic.setBasic(basic); switchPortBasic.setBasic(basic);
portProfile.getVlanProfile().setSwitchportBasic(switchPortBasic); portProfile.getVlanProfile().setSwitchportBasic(switchPortBasic);
// configure L2 mode for vlan sub-profile // configure L2 mode for vlan sub-profile
if (executeUpdateObject(portProfile, Constants.URI)) { if (executeUpdateObject(portProfile, Constants.URI)) {
VlanProfile vlanProfile = new VlanProfile(); VlanProfile vlanProfile = new VlanProfile();
Switchport switchPort = new Switchport(); Switchport switchPort = new Switchport();
Mode mode = new Mode(); final Mode mode = new Mode();
mode.setVlanMode("trunk"); mode.setVlanMode("trunk");
switchPort.setMode(mode); switchPort.setMode(mode);
vlanProfile.setSwitchport(switchPort); vlanProfile.setSwitchport(switchPort);
@ -191,9 +190,9 @@ public class BrocadeVcsApi {
if (executeUpdateObject(portProfile, Constants.URI)) { if (executeUpdateObject(portProfile, Constants.URI)) {
vlanProfile = new VlanProfile(); vlanProfile = new VlanProfile();
switchPort = new Switchport(); switchPort = new Switchport();
Trunk trunk = new Trunk(); final Trunk trunk = new Trunk();
Allowed allowed = new Allowed(); final Allowed allowed = new Allowed();
Allowed.Vlan allowedVlan = new Allowed.Vlan(); final Allowed.Vlan allowedVlan = new Allowed.Vlan();
allowedVlan.setAdd(vlanId); allowedVlan.setAdd(vlanId);
allowed.setVlan(allowedVlan); allowed.setVlan(allowedVlan);
trunk.setAllowed(allowed); trunk.setAllowed(allowed);
@ -214,9 +213,9 @@ public class BrocadeVcsApi {
* Creates a vlan interface. * Creates a vlan interface.
*/ */
private boolean createInterfaceVlan(int vlanId) throws BrocadeVcsApiException { private boolean createInterfaceVlan(int vlanId) throws BrocadeVcsApiException {
InterfaceVlan interfaceVlan = new InterfaceVlan(); final InterfaceVlan interfaceVlan = new InterfaceVlan();
Interface interfaceObj = new Interface(); final Interface interfaceObj = new Interface();
Vlan vlan = new Vlan(); final Vlan vlan = new Vlan();
vlan.setName(vlanId); vlan.setName(vlanId);
interfaceObj.setVlan(vlan); interfaceObj.setVlan(vlan);
interfaceVlan.setInterface(interfaceObj); interfaceVlan.setInterface(interfaceObj);
@ -230,10 +229,10 @@ public class BrocadeVcsApi {
*/ */
public boolean associateMacToNetwork(long networkId, String macAddress) throws BrocadeVcsApiException { public boolean associateMacToNetwork(long networkId, String macAddress) throws BrocadeVcsApiException {
PortProfileGlobal portProfileGlobal = new PortProfileGlobal(); final PortProfileGlobal portProfileGlobal = new PortProfileGlobal();
PortProfile portProfile = new PortProfile(); final PortProfile portProfile = new PortProfile();
portProfile.setName(Constants.PORT_PROFILE_NAME_PREFIX + networkId); portProfile.setName(Constants.PORT_PROFILE_NAME_PREFIX + networkId);
Static staticObj = new Static(); final Static staticObj = new Static();
staticObj.setMacAddress(macAddress); staticObj.setMacAddress(macAddress);
portProfile.setStatic(staticObj); portProfile.setStatic(staticObj);
portProfileGlobal.setPortProfile(portProfile); portProfileGlobal.setPortProfile(portProfile);
@ -247,10 +246,10 @@ public class BrocadeVcsApi {
*/ */
public boolean disassociateMacFromNetwork(long networkId, String macAddress) throws BrocadeVcsApiException { public boolean disassociateMacFromNetwork(long networkId, String macAddress) throws BrocadeVcsApiException {
PortProfileGlobal portProfileGlobal = new PortProfileGlobal(); final PortProfileGlobal portProfileGlobal = new PortProfileGlobal();
PortProfile portProfile = new PortProfile(); final PortProfile portProfile = new PortProfile();
portProfile.setName(Constants.PORT_PROFILE_NAME_PREFIX + networkId); portProfile.setName(Constants.PORT_PROFILE_NAME_PREFIX + networkId);
Static staticObj = new Static(); final Static staticObj = new Static();
staticObj.setOperation("delete"); staticObj.setOperation("delete");
staticObj.setMacAddress(macAddress); staticObj.setMacAddress(macAddress);
portProfile.setStatic(staticObj); portProfile.setStatic(staticObj);
@ -278,9 +277,9 @@ public class BrocadeVcsApi {
* Deletes a vlan interface. * Deletes a vlan interface.
*/ */
private boolean deleteInterfaceVlan(int vlanId) throws BrocadeVcsApiException { private boolean deleteInterfaceVlan(int vlanId) throws BrocadeVcsApiException {
InterfaceVlan interfaceVlan = new InterfaceVlan(); final InterfaceVlan interfaceVlan = new InterfaceVlan();
Interface interfaceObj = new Interface(); final Interface interfaceObj = new Interface();
Vlan vlan = new Vlan(); final Vlan vlan = new Vlan();
vlan.setOperation("delete"); vlan.setOperation("delete");
vlan.setName(vlanId); vlan.setName(vlanId);
interfaceObj.setVlan(vlan); interfaceObj.setVlan(vlan);
@ -294,10 +293,10 @@ public class BrocadeVcsApi {
* Deactivates a port-profile. * Deactivates a port-profile.
*/ */
private boolean deactivatePortProfile(long networkId) throws BrocadeVcsApiException { private boolean deactivatePortProfile(long networkId) throws BrocadeVcsApiException {
PortProfileGlobal portProfileGlobal = new PortProfileGlobal(); final PortProfileGlobal portProfileGlobal = new PortProfileGlobal();
PortProfile portProfile = new PortProfile(); final PortProfile portProfile = new PortProfile();
portProfile.setName(Constants.PORT_PROFILE_NAME_PREFIX + networkId); portProfile.setName(Constants.PORT_PROFILE_NAME_PREFIX + networkId);
Activate activate = new Activate(); final Activate activate = new Activate();
activate.setOperation("delete"); activate.setOperation("delete");
portProfile.setActivate(activate); portProfile.setActivate(activate);
portProfileGlobal.setPortProfile(portProfile); portProfileGlobal.setPortProfile(portProfile);
@ -311,7 +310,7 @@ public class BrocadeVcsApi {
*/ */
private boolean deletePortProfile(long networkId) throws BrocadeVcsApiException { private boolean deletePortProfile(long networkId) throws BrocadeVcsApiException {
PortProfile portProfile = new PortProfile(); final PortProfile portProfile = new PortProfile();
portProfile.setName(Constants.PORT_PROFILE_NAME_PREFIX + networkId); portProfile.setName(Constants.PORT_PROFILE_NAME_PREFIX + networkId);
portProfile.setOperation("delete"); portProfile.setOperation("delete");
//deletes port-profile //deletes port-profile
@ -320,25 +319,25 @@ public class BrocadeVcsApi {
protected <T> boolean executeUpdateObject(T newObject, String uri) throws BrocadeVcsApiException { protected <T> boolean executeUpdateObject(T newObject, String uri) throws BrocadeVcsApiException {
boolean result = true; final boolean result = true;
if (_host == null || _host.isEmpty() || _adminuser == null || _adminuser.isEmpty() || _adminpass == null || _adminpass.isEmpty()) { if (_host == null || _host.isEmpty() || _adminuser == null || _adminuser.isEmpty() || _adminpass == null || _adminpass.isEmpty()) {
throw new BrocadeVcsApiException("Hostname/credentials are null or empty"); throw new BrocadeVcsApiException("Hostname/credentials are null or empty");
} }
HttpPatch pm = (HttpPatch)createMethod("patch", uri); final HttpPatch pm = (HttpPatch)createMethod("patch", uri);
pm.setHeader("Accept", "application/vnd.configuration.resource+xml"); pm.setHeader("Accept", "application/vnd.configuration.resource+xml");
pm.setEntity(new StringEntity(convertToString(newObject), ContentType.APPLICATION_XML)); pm.setEntity(new StringEntity(convertToString(newObject), ContentType.APPLICATION_XML));
HttpResponse response = executeMethod(pm); final HttpResponse response = executeMethod(pm);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_NO_CONTENT) { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_NO_CONTENT) {
String errorMessage; String errorMessage;
try { try {
errorMessage = responseToErrorMessage(response); errorMessage = responseToErrorMessage(response);
} catch (IOException e) { } catch (final IOException e) {
s_logger.error("Failed to update object : " + e.getMessage()); s_logger.error("Failed to update object : " + e.getMessage());
throw new BrocadeVcsApiException("Failed to update object : " + e.getMessage()); throw new BrocadeVcsApiException("Failed to update object : " + e.getMessage());
} }
@ -363,12 +362,12 @@ public class BrocadeVcsApi {
marshaller.marshal(object, stringWriter); marshaller.marshal(object, stringWriter);
} catch (JAXBException e) { } catch (final JAXBException e) {
s_logger.error("Failed to convert object to string : " + e.getMessage()); s_logger.error("Failed to convert object to string : " + e.getMessage());
throw new BrocadeVcsApiException("Failed to convert object to string : " + e.getMessage()); throw new BrocadeVcsApiException("Failed to convert object to string : " + e.getMessage());
} }
String str = stringWriter.toString(); final String str = stringWriter.toString();
s_logger.info(str); s_logger.info(str);
return str; return str;
@ -379,19 +378,19 @@ public class BrocadeVcsApi {
Output output = null; Output output = null;
try { try {
JAXBContext context = JAXBContext.newInstance(Output.class); final JAXBContext context = JAXBContext.newInstance(Output.class);
StringReader reader = new StringReader(object); final StringReader reader = new StringReader(object);
final Unmarshaller unmarshaller = context.createUnmarshaller(); final Unmarshaller unmarshaller = context.createUnmarshaller();
Object result = unmarshaller.unmarshal(reader); final Object result = unmarshaller.unmarshal(reader);
if (result instanceof Output) { if (result instanceof Output) {
output = (Output)result; output = (Output)result;
s_logger.info(output); s_logger.info(output);
} }
} catch (JAXBException e) { } catch (final JAXBException e) {
s_logger.error("Failed to convert string to object : " + e.getMessage()); s_logger.error("Failed to convert string to object : " + e.getMessage());
throw new BrocadeVcsApiException("Failed to convert string to object : " + e.getMessage()); throw new BrocadeVcsApiException("Failed to convert string to object : " + e.getMessage());
} }
@ -405,19 +404,19 @@ public class BrocadeVcsApi {
throw new BrocadeVcsApiException("Hostname/credentials are null or empty"); throw new BrocadeVcsApiException("Hostname/credentials are null or empty");
} }
boolean result = true; final boolean result = true;
HttpPost pm = (HttpPost)createMethod("post", uri); final HttpPost pm = (HttpPost)createMethod("post", uri);
pm.setHeader("Accept", "application/vnd.configuration.resource+xml"); pm.setHeader("Accept", "application/vnd.configuration.resource+xml");
pm.setEntity(new StringEntity(convertToString(newObject), ContentType.APPLICATION_XML)); pm.setEntity(new StringEntity(convertToString(newObject), ContentType.APPLICATION_XML));
HttpResponse response = executeMethod(pm); final HttpResponse response = executeMethod(pm);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) {
String errorMessage; String errorMessage;
try { try {
errorMessage = responseToErrorMessage(response); errorMessage = responseToErrorMessage(response);
} catch (IOException e) { } catch (final IOException e) {
s_logger.error("Failed to create object : " + e.getMessage()); s_logger.error("Failed to create object : " + e.getMessage());
throw new BrocadeVcsApiException("Failed to create object : " + e.getMessage()); throw new BrocadeVcsApiException("Failed to create object : " + e.getMessage());
} }
@ -440,18 +439,18 @@ public class BrocadeVcsApi {
String readLine = null; String readLine = null;
StringBuffer sb = null; StringBuffer sb = null;
HttpPost pm = (HttpPost)createMethod("post", uri); final HttpPost pm = (HttpPost)createMethod("post", uri);
pm.setHeader("Accept", "application/vnd.operational-state.resource+xml"); pm.setHeader("Accept", "application/vnd.operational-state.resource+xml");
pm.setEntity(new StringEntity("<show-vcs></show-vcs>", ContentType.APPLICATION_XML)); pm.setEntity(new StringEntity("<show-vcs></show-vcs>", ContentType.APPLICATION_XML));
HttpResponse response = executeMethod(pm); final HttpResponse response = executeMethod(pm);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
String errorMessage; String errorMessage;
try { try {
errorMessage = responseToErrorMessage(response); errorMessage = responseToErrorMessage(response);
} catch (IOException e) { } catch (final IOException e) {
s_logger.error("Failed to retreive status : " + e.getMessage()); s_logger.error("Failed to retreive status : " + e.getMessage());
throw new BrocadeVcsApiException("Failed to retreive status : " + e.getMessage()); throw new BrocadeVcsApiException("Failed to retreive status : " + e.getMessage());
} }
@ -464,12 +463,12 @@ public class BrocadeVcsApi {
try (BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), Charset.forName("UTF-8")))) { try (BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), Charset.forName("UTF-8")))) {
sb = new StringBuffer(); sb = new StringBuffer();
while (((readLine = br.readLine()) != null)) { while ((readLine = br.readLine()) != null) {
s_logger.debug(readLine); s_logger.debug(readLine);
sb.append(readLine); sb.append(readLine);
} }
} catch (Exception e) { } catch (final Exception e) {
s_logger.error("Failed to retreive status : " + e.getMessage()); s_logger.error("Failed to retreive status : " + e.getMessage());
throw new BrocadeVcsApiException("Failed to retreive status : " + e.getMessage()); throw new BrocadeVcsApiException("Failed to retreive status : " + e.getMessage());
} }
@ -484,17 +483,17 @@ public class BrocadeVcsApi {
throw new BrocadeVcsApiException("Hostname/credentials are null or empty"); throw new BrocadeVcsApiException("Hostname/credentials are null or empty");
} }
HttpDelete dm = (HttpDelete)createMethod("delete", uri); final HttpDelete dm = (HttpDelete)createMethod("delete", uri);
dm.setHeader("Accept", "application/vnd.configuration.resource+xml"); dm.setHeader("Accept", "application/vnd.configuration.resource+xml");
HttpResponse response = executeMethod(dm); final HttpResponse response = executeMethod(dm);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_NO_CONTENT) { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_NO_CONTENT) {
String errorMessage; String errorMessage;
try { try {
errorMessage = responseToErrorMessage(response); errorMessage = responseToErrorMessage(response);
} catch (IOException e) { } catch (final IOException e) {
s_logger.error("Failed to delete object : " + e.getMessage()); s_logger.error("Failed to delete object : " + e.getMessage());
throw new BrocadeVcsApiException("Failed to delete object : " + e.getMessage()); throw new BrocadeVcsApiException("Failed to delete object : " + e.getMessage());
} }
@ -514,11 +513,7 @@ public class BrocadeVcsApi {
method.releaseConnection(); method.releaseConnection();
response = _client.execute(method); response = _client.execute(method);
} }
} catch (HttpException e) { } catch (final IOException e) {
s_logger.error("HttpException caught while trying to connect to the Brocade Switch", e);
method.releaseConnection();
throw new BrocadeVcsApiException("API call to Brocade Switch Failed", e);
} catch (IOException e) {
s_logger.error("IOException caught while trying to connect to the Brocade Switch", e); s_logger.error("IOException caught while trying to connect to the Brocade Switch", e);
method.releaseConnection(); method.releaseConnection();
throw new BrocadeVcsApiException("API call to Brocade Switch Failed", e); throw new BrocadeVcsApiException("API call to Brocade Switch Failed", e);
@ -533,7 +528,7 @@ public class BrocadeVcsApi {
try (BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), Charset.forName("UTF-8")))) { try (BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), Charset.forName("UTF-8")))) {
StringBuffer result = new StringBuffer(); final StringBuffer result = new StringBuffer();
String line = ""; String line = "";
while ((line = rd.readLine()) != null) { while ((line = rd.readLine()) != null) {
result.append(line); result.append(line);

View File

@ -26,9 +26,9 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.io.IOException; import java.io.IOException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.HttpPatch; import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
@ -93,7 +93,7 @@ public class BrocadeVcsApiTest {
when(response.getEntity()).thenReturn(new StringEntity(OUTPUT_XML_RESPONSE)); when(response.getEntity()).thenReturn(new StringEntity(OUTPUT_XML_RESPONSE));
// Execute // Execute
Output result = api.getSwitchStatus(); final Output result = api.getSwitchStatus();
// Assert // Assert
verify(method, times(1)).releaseConnection(); verify(method, times(1)).releaseConnection();

View File

@ -33,9 +33,9 @@ import static org.mockito.Mockito.when;
import java.util.List; import java.util.List;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.HttpRequest; import org.apache.http.HttpRequest;
import org.apache.http.HttpStatus;
import org.apache.http.ProtocolVersion; import org.apache.http.ProtocolVersion;
import org.apache.http.StatusLine; import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;