Skip to content
This repository has been archived by the owner on Mar 9, 2021. It is now read-only.

Commit

Permalink
fix #1
Browse files Browse the repository at this point in the history
add some resource management
  • Loading branch information
mirthas committed Apr 16, 2015
1 parent 6380260 commit 3cab8f5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
Binary file modified lib/fritztr064.jar
Binary file not shown.
50 changes: 39 additions & 11 deletions src/de/mapoll/javaAVMTR064/FritzConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
***********************************************************************************************************************/
package de.mapoll.javaAVMTR064;



import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
Expand All @@ -30,8 +33,13 @@
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;





import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
Expand All @@ -46,6 +54,8 @@
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

import de.mapoll.javaAVMTR064.beans.DeviceType;
import de.mapoll.javaAVMTR064.beans.RootType;
Expand Down Expand Up @@ -141,13 +151,36 @@ private void getServicesFromDevice(DeviceType device) throws IOException, JAXBEx
}
}

private InputStream httpRequest(HttpHost target, HttpRequest request, HttpContext context) throws IOException{
CloseableHttpResponse response = null;
byte[] content = null;
try {
response = httpClient.execute(target, request, context);
content = EntityUtils.toByteArray(response.getEntity());
} catch (IOException e) {
throw e;
}
finally{
if(response != null){
response.close();
if (response.getStatusLine().getStatusCode()!=200){
throw new IOException(response.getStatusLine().toString());
}
}

}
if (content != null)
return new ByteArrayInputStream(content);
else
return new ByteArrayInputStream(new byte[0]);
}



protected InputStream getXMLIS(String fileName) throws IOException{
HttpGet httpget = new HttpGet(fileName);
CloseableHttpResponse response = httpClient.execute(targetHost, httpget, context);
if (response.getStatusLine().getStatusCode()!=200){
throw new IOException(response.getStatusLine().toString());
}
return response.getEntity().getContent();
return httpRequest(targetHost, httpget, context);

}

protected InputStream getSOAPXMLIS(String fileName, String urn, HttpEntity entity) throws IOException{
Expand All @@ -156,12 +189,7 @@ protected InputStream getSOAPXMLIS(String fileName, String urn, HttpEntity entit
httppost.addHeader("charset","utf-8");
httppost.addHeader("content-type","text/xml");
httppost.setEntity(entity);

CloseableHttpResponse response = httpClient.execute(targetHost, httppost, context);
if (response.getStatusLine().getStatusCode()!=200){
throw new IOException(response.getStatusLine().toString());
}
return response.getEntity().getContent();
return httpRequest(targetHost, httppost, context);
}


Expand Down

0 comments on commit 3cab8f5

Please sign in to comment.