Permalink
Browse files

exception handling improved if create-job failed

  • Loading branch information...
oboehm
oboehm committed Dec 13, 2018
1 parent f4e7825 commit f336c25161b9153cdf74f8f11a3e61894aa14fa4
@@ -103,5 +103,10 @@ public int getHttpStatusCode() {
public void setHttpStatusCode(int httpStatusCode) {
this.httpStatusCode = httpStatusCode;
}


@Override
public String toString() {
return httpStatusCode + " (" + httpStatusResponse + ")";
}

}
@@ -1,38 +1,30 @@
package org.cups4j;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ch.ethz.vppserver.ippclient.IppResult;
import org.cups4j.ipp.ResponseException;
import org.cups4j.ipp.attributes.Attribute;
import org.cups4j.ipp.attributes.AttributeGroup;
import org.cups4j.operations.ipp.IppCreateJobOperation;
import org.cups4j.operations.ipp.IppGetJobAttributesOperation;
import org.cups4j.operations.ipp.IppGetJobsOperation;
import org.cups4j.operations.ipp.IppPrintJobOperation;
import org.cups4j.operations.ipp.IppSendDocumentOperation;
import org.cups4j.operations.ipp.*;

import java.io.InputStream;
import java.net.URL;
import java.util.*;

/**
* Copyright (C) 2009 Harald Weyhing
*
* <p>
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU Lesser General Public License as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* <p>
* 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.
*
* <p>
* See the GNU Lesser General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see
* <http://www.gnu.org/licenses/>.
*/

import ch.ethz.vppserver.ippclient.IppResult;

/**
* Represents a printer on your IPP server
*/
@@ -270,8 +262,12 @@ public int createJob(PrintJob job) {
attributes.put("requesting-user-name", job.getUserName());
IppCreateJobOperation command = new IppCreateJobOperation(printerURL.getPort());
IppResult ippResult = command.request(printerURL, attributes);
AttributeGroup attrGroup = ippResult.getAttributeGroup("job-attributes-tag");
return Integer.parseInt(attrGroup.getAttribute("job-id").getValue());
if (ippResult.getHttpStatusCode() == 200) {
AttributeGroup attrGroup = ippResult.getAttributeGroup("job-attributes-tag");
return Integer.parseInt(attrGroup.getAttribute("job-id").getValue());
} else {
throw new ResponseException(command, ippResult);
}
}

/**
@@ -0,0 +1,26 @@
package org.cups4j.ipp;

import ch.ethz.vppserver.ippclient.IppResult;
import org.cups4j.operations.IppOperation;

/**
* The class ResponseException will be thrown if we will get an unexpected
* response.
*
* @author oboehm
* @since 0.7.6 (13.12.2018)
*/
public final class ResponseException extends RuntimeException {

/**
* Constructs a new response exception with the specified detail message
* from the given operation and response.
*
* @param op the operation
* @param ippResult the cause of the exception
*/
public ResponseException(IppOperation op, IppResult ippResult) {
super(op + " failed with " + ippResult);
}

}
@@ -260,4 +260,9 @@ protected String getAttributeValue(Attribute attr) {
return attr.getAttributeValue().get(0).getValue();
}

@Override
public String toString() {
return this.getClass().getSimpleName() + ":" + ippPort;
}

}
@@ -0,0 +1,40 @@
package org.cups4j.ipp;

import ch.ethz.vppserver.ippclient.IppResult;
import org.cups4j.operations.IppOperation;
import org.cups4j.operations.ipp.IppCreateJobOperation;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;

/**
* Unit tests for {@link ResponseException}.
*
* @author: oboehm
*/
public final class ResponseExceptionTest {

private static final Logger LOG = LoggerFactory.getLogger(ResponseExceptionTest.class);

@Test
public void testGetMessage() {
IppOperation op = new IppCreateJobOperation();
IppResult result = createIppResult(400, "Bad Request");
ResponseException ex = new ResponseException(op, result);
assertThat(ex.getMessage(), containsString("Bad Request"));
LOG.info("msg = \"{}\"", ex.getMessage());
}

private static IppResult createIppResult(int sc, String response) {
IppResult result = new IppResult();
result.setHttpStatusCode(sc);
result.setHttpStatusResponse(response);
result.setIppStatusResponse("Major Version:0x02 Minor Version:0x00 Request Id:3\n" +
"Status Code:0x0400(client-error-bad-request)");
return result;
}

}

0 comments on commit f336c25

Please sign in to comment.