Permalink
Browse files

- changed version to 0.7.2-SNAPSHOT

- throws Exception when HTTP status code>=300
- minor refactoring
  • Loading branch information...
harwey committed Feb 15, 2018
1 parent f6722d7 commit f770f705135ddbc576670daee44250a01eb3a01a
View
@@ -4,7 +4,7 @@
<groupId>org.cups4j</groupId>
<artifactId>cups4j</artifactId>
<version>0.7.1</version>
<version>0.7.2-SNAPSHOT</version>
<packaging>jar</packaging>
<name>cups4j</name>
@@ -21,9 +21,10 @@
* program; if not, see <http://www.gnu.org/licenses/>.
*/
public class IppResult {
String _httpStatusResponse = null;
String _ippStatusResponse = null;
List<AttributeGroup> _attributeGroupList = null;
private String httpStatusResponse = null;
private String ippStatusResponse = null;
private List<AttributeGroup> attributeGroupList = null;
private int httpStatusCode;
public IppResult() {
}
@@ -33,46 +34,55 @@ public IppResult() {
* @return
*/
public String getHttpStatusResponse() {
return _httpStatusResponse;
return httpStatusResponse;
}
/**
*
* @param statusResponse
*/
public void setHttpStatusResponse(String statusResponse) {
_httpStatusResponse = statusResponse;
httpStatusResponse = statusResponse;
}
/**
*
* @return
*/
public String getIppStatusResponse() {
return _ippStatusResponse;
return ippStatusResponse;
}
/**
*
* @param statusResponse
*/
public void setIppStatusResponse(String statusResponse) {
_ippStatusResponse = statusResponse;
ippStatusResponse = statusResponse;
}
/**
*
* @return
*/
public List<AttributeGroup> getAttributeGroupList() {
return _attributeGroupList;
return attributeGroupList;
}
/**
*
* @param group
*/
public void setAttributeGroupList(List<AttributeGroup> group) {
_attributeGroupList = group;
attributeGroupList = group;
}
public int getHttpStatusCode() {
return httpStatusCode;
}
public void setHttpStatusCode(int httpStatusCode) {
this.httpStatusCode = httpStatusCode;
}
}
@@ -50,12 +50,13 @@
private final static String IPP_MIME_TYPE = "application/ipp";
private HttpPost httpCall;
//
private String httpStatusLine = null;
private int httpStatusCode = -1;
private static final Logger LOG = LoggerFactory.getLogger(IppOperation.class);
/**
* Gets the IPP header
*
@@ -129,11 +130,14 @@ public ByteBuffer getIppHeader(URL url, Map<String, String> map) throws Unsuppor
* @param ippBuf
*
* @return result
* @throws IOException
* @throws JAXBException
* @throws Exception
*/
private IppResult sendRequest(URL url, ByteBuffer ippBuf) throws Exception {
return sendRequest(url, ippBuf, null);
private IppResult sendRequest(URL url, ByteBuffer ippBuf) throws Exception {
IppResult result = sendRequest(url, ippBuf, null);
if (result.getHttpStatusCode() >= 300) {
throw new IOException("HTTP error! Status code: " + result.getHttpStatusResponse());
}
return result;
}
/**
@@ -156,33 +160,34 @@ private IppResult sendRequest(URL url, ByteBuffer ippBuf, InputStream documentSt
return null;
}
// HttpClient client = new DefaultHttpClient();
//
// // will not work with older versions of CUPS!
// client.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
// client.getParams().setParameter("http.socket.timeout", new Integer(10000));
// client.getParams().setParameter("http.connection.timeout", new Integer(10000));
// client.getParams().setParameter("http.protocol.content-charset", "UTF-8");
// client.getParams().setParameter("http.method.response.buffer.warnlimit", new Integer(8092));
//
// // probabaly not working with older CUPS versions
// client.getParams().setParameter("http.protocol.expect-continue", Boolean.valueOf(true));
// HttpClient client = new DefaultHttpClient();
//
// // will not work with older versions of CUPS!
// client.getParams().setParameter("http.protocol.version",
// HttpVersion.HTTP_1_1);
// client.getParams().setParameter("http.socket.timeout", new
// Integer(10000));
// client.getParams().setParameter("http.connection.timeout", new
// Integer(10000));
// client.getParams().setParameter("http.protocol.content-charset",
// "UTF-8");
// client.getParams().setParameter("http.method.response.buffer.warnlimit",
// new Integer(8092));
//
// // probabaly not working with older CUPS versions
// client.getParams().setParameter("http.protocol.expect-continue",
// Boolean.valueOf(true));
HttpClient client = HttpClientBuilder.create().build();
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(10000)
.setConnectTimeout(10000)
.build();
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build();
HttpPost httpPost = new HttpPost(new URI("http://" + url.getHost() + ":" + ippPort) + url.getPath());
httpPost.setConfig(requestConfig);
httpCall = httpPost;
// httpPost.getParams().setParameter("http.socket.timeout", new Integer(10000));
// httpPost.getParams().setParameter("http.socket.timeout", new
// Integer(10000));
byte[] bytes = new byte[ippBuf.limit()];
ippBuf.get(bytes);
@@ -201,11 +206,13 @@ private IppResult sendRequest(URL url, ByteBuffer ippBuf, InputStream documentSt
httpPost.setEntity(requestEntity);
httpStatusLine = null;
httpStatusCode = -1;
ResponseHandler<byte[]> handler = new ResponseHandler<byte[]>() {
public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
HttpEntity entity = response.getEntity();
httpStatusLine = response.getStatusLine().toString();
httpStatusCode = response.getStatusLine().getStatusCode();
if (entity != null) {
return EntityUtils.toByteArray(entity);
} else {
@@ -220,6 +227,7 @@ private IppResult sendRequest(URL url, ByteBuffer ippBuf, InputStream documentSt
ippResult = ippResponse.getResponse(ByteBuffer.wrap(result));
ippResult.setHttpStatusResponse(httpStatusLine);
ippResult.setHttpStatusCode(httpStatusCode);
// IppResultPrinter.print(ippResult);

0 comments on commit f770f70

Please sign in to comment.