I have a Java application that uses the Google Cloud Print API (https://github.com/jittagornp/GoogleCloudPrint) to print pdf's. In most cases it works like a charm but every now and then (approx 1 out of 100) it gives an error back which looks like this "Det går inte att göra ändringar i ett avslutat jobb".
Roughly translated to english it reads: "Can't make changes to a finished job"
The kicker is that the pages prints perfectly every time, with or without the error.
Code looks like this for every call to GCP:
public static SubmitJobResponse submitPrintJob(String filename, String type){
String jsonTicket;
String printerId;
SubmitJobResponse response = null;
String jobtitle;
jobtitle = filename.substring(filename.lastIndexOf('/') + 1);
// Login to GCP
GoogleCloudPrint cloudPrint = gcpLogin();
jsonTicket = "{'version':'1.0','print':{'vendor_ticket_item':[],'color':{'type':1},'copies':{'copies':4}}}";
printerId = session("printerlabel");
try {
// Get file as byte array
Path path = Paths.get(filename);
byte[] content = Files.readAllBytes(path);
Gson gson = new Gson();
Ticket ticket = gson.fromJson(jsonTicket, Ticket.class);
String json = gson.toJson(ticket);
//create job
SubmitJob submitJob = new SubmitJob();
submitJob.setContent(content);
submitJob.setContentType("application/pdf");
submitJob.setPrinterId(printerId);
submitJob.setTicket(ticket);
submitJob.setTitle(jobtitle);
//send job
response = cloudPrint.submitJob(submitJob);
Logger.debug("PrinterID => {}", printerId);
Logger.debug("submit job response => {}", response.isSuccess() + "," + response.getMessage());
if(response.isSuccess()) {
Logger.debug("submit job id => {}", response.getJob().getId());
}
cloudPrint.disconnect();
} catch (Exception ex) {
Logger.warn(null, ex);
}
return response;
}
The API uses a call to "/submit?output=json&printerid=" at GCP so why does it say that I'm trying to change a finished job - I'm not submitting a job id - only printer id.
How do I get rid of these false errors?
Related
When doing calls to Exact-on-line API to get authenticated we run into the problem that getting the first refresh-token fails. We're not sure why. This is what we get back from Exact:
Http code: 400
JSON Data:
{
error='invalid_request',
description='Handle could not be extracted',
uri='null',
state='null',
scope='null',
redirectUri='null',
responseStatus=400,
parameters={}
}
We use this Java code based on library org.apache.oltu.oauth2.client (1.0.2):
OAuthClientRequest oAuthRequest = OAuthClientRequest //
.tokenLocation(BASE_URL + "/api/oauth2/token") //
.setGrantType(GrantType.AUTHORIZATION_CODE) //
.setClientId(clientId) //
.setClientSecret(clientSecret) //
.setRedirectURI(REDIRECT_URI) //
.setCode(code) //
.buildBodyMessage();
OAuthClient client = new OAuthClient(new URLConnectionClient());
OAuthJSONAccessTokenResponse oauthResponse = client.accessToken(oAuthRequest, OAuth.HttpMethod.POST);
We did do the first step (getting the 'code' as used in setCode(...)) using a localhost-redirect as displayed in https://support.exactonline.com/community/s/knowledge-base#All-All-DNO-Content-gettingstarted There we copy the code from the address-bar of our browser and store it in a place the next computer-step can read it again.
This is due to the fact that the code was copied from your browsers address-bar. There you will find a URL-encoded version of the code (visible in the '%21' often) which when passed into the setCode verbatim will fail the subsequent calls.
Suggestion: URL-decode the value or setup a small temporary localhost-HTTP-server using Undertow or the like to catch the code that was send to you localhost-URL:
Undertow server = Undertow.builder() //
.addHttpListener(7891, "localhost") //
.setHandler(new HttpHandler() {
#Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
String code = exchange.getQueryParameters().get("code").getFirst();
LOG.info("Recieved code: {}.", code);
LOG.info("Store code");
storeCode(code);
LOG.info("Code stored");
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
exchange.getResponseSender().send( //
"Thanks for getting me the code: " + code + "\n" //
+ "Will store it for you and get the first refreshToken..." //
+ "Please have a look at " + OAUTH_STATE_INI
+ " for the new code & refreshToken in a minute" //
);
done.add("done");
}
}).build();
server.start();
NB: Do make sure the redirect URL is correct in your Exact-app-settings
I'm programming in Java and I'm trying to fetch all the Facebook groups which I am the admin.
Thus, I've created an app called "Secrets" in Facebook Graph API Explorer and
checked all the permissions when I generated my ACCESS TOKEN.
For my Java app I'm using the library: restfb-1.18.0
The weird thing is that I'm getting the list of my admined groups but then
the app throws the exception: OAuthException: (#200) Permissions error (code 200, subcode null)
I don't know why i get this error.
Here is my code snippet:
String accessTokenGraphApiExplorer = "...";
FacebookClient fbClient = new DefaultFacebookClient(accessTokenGraphApiExplorer,Version.VERSION_2_0);
int counter = 0;
final String MY_FEEDS_PROPERTY = "me/groups";
String param = "groups,name";
Connection<Group> result = fbClient.fetchConnection(MY_FEEDS_PROPERTY, Group.class, Parameter.with("fields", param));
try {
for (List<Group> groupPage : result) {
for (Group group : groupPage) {
System.out.println(group.getName());
counter++;
}
}
System.out.println("# of Groups I admin: " + counter);
}
catch (Exception e) {
System.out.println(e);
}
How can I fix it ?
I am trying to retrieve leaves of a MIB tree with the SNMP-walk command using the Java code below, with no success. I am using the SNMP4J framework to interact with the other SNMP end of the application.
My test case is to retrieve the maxPhases scalar from the MIB, which is a parameter inside the NTCIP protocol (transportation system protocol from the US government).
The other end of the application is built using the net-snmp framework (C/C++), where a use case is shown in the RESULTDS.
The example image can retrieve the maxPhases and the maxPhaseGroups values (both INTEGER 24).
I am using SHA for authentication and AES128 to encrypt the data (system will be deployed using the internet, so we need the security layer).
public static String doSnmpwalk() throws IOException {
Snmp snmp = null;
TransportMapping transport = null;
try {
Address targetAddress = GenericAddress.parse("udp:" + targetAddr + "/" + portNum);
transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
SecurityModels.getInstance().addSecurityModel(usm);
snmp.getUSM().addUser(new OctetString("user"),
new UsmUser(new OctetString("SHAAES"), AuthSHA.ID, new OctetString("12345678"), PrivAES128.ID, new OctetString("91234567")));
transport.listen();
UserTarget target = new UserTarget();
target.setAddress(targetAddress);
target.setRetries(1);
target.setTimeout(5000);
target.setVersion(SnmpConstants.version3);
target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
target.setSecurityName(new OctetString("SHAAES"));
OID oid = new OID(oidStr);
OID [] oidArray = new OID[1];
oidArray[0] = oid;
DefaultPDUFactory defaultPDUFactory = new DefaultPDUFactory(PDU.GETBULK);
TreeUtils treeUtils = new TreeUtils(snmp, defaultPDUFactory);
//List<TreeEvent> events = treeUtils.getSubtree(target, oid);
List<TreeEvent> events = treeUtils.walk(target, oidArray);
if (events == null || events.size() == 0) {
return null;
}
// Get snmpwalk result.
for (TreeEvent event : events) {
if (event.isError()) {
// LOGGER.error("OID has an error: {}", event.getErrorMessage());
} else {
VariableBinding[] varBindings = event.getVariableBindings();
if (varBindings == null || varBindings.length == 0) {
//System.out.println("VarBinding: No result returned.");
}
for (VariableBinding varBinding : varBindings) {
varBinding.toValueString();
results += (varBinding.getOid().toString() + "=" + varBinding.getVariable().toString()) + "\n";
}
}
}
} finally {
if (snmp!=null) snmp.close();
if (transport!=null) transport.close();
}
return results;
}
Results:
SNMPv2-SMI::enterprises.1206.4.2.1.1.1.0 = INTEGER: 24
SNMPv2-SMI::enterprises.1206.4.2.1.1.3.0 = INTEGER: 24
The question was wrongly phrased, because SNMP4J actually returns the leaf objects' values. The author of the question, according to his last comment above, is looking for the MIB information associated with the object ID he gets back.
That info is no transferred over the wire, but is static information from the corresponding MIB specification. SNMP4J-SMI-PRO or other MIB parsers can be used to get that info too. SNMP4J-SMI-PRO will do that seamlessly when the content of the Variable or VariableBinding is converted into a string. It uses the DISPLAY-HINT information and the OBJECT-TYPE SYNTAX from the MIB specification for that.
I am trying to upload CSV file, which is in the right format. If I try to upload only 3 rows of data, then it works, but for large file sometimes it says socket-timeout exception our just job DONE. This dataset is in other location.
Bigquery bigquery = GoogleConnector.getBigQuery();
TableSchema schema = new TableSchema();
List<TableFieldSchema> tableFieldSchema =
getTableFieldSchema("mostRecentObservation");
schema.setFields(tableFieldSchema);
Table table = new Table();
table.setSchema(schema);
TableReference tableRef = new TableReference();
tableRef.setDatasetId("MetroMallsEU");
tableRef.setProjectId(projectId);
tableRef.setTableId("mostRecentObsTest");
table.setTableReference(tableRef);
FileContent content = new FileContent("application/octet-stream",
new File(fileNameToLoad));
Job job = new Job();
JobConfiguration config = new JobConfiguration();
JobConfigurationLoad configLoad = new JobConfigurationLoad();
configLoad.setSchema(schema);
configLoad.setDestinationTable(tableRef);
configLoad.setEncoding("UTF-8");
configLoad.setCreateDisposition("CREATE_IF_NEEDED");
configLoad.setSourceFormat("CSV");
// configLoad.setSkipLeadingRows(1);
config.setLoad(configLoad);
job.setConfiguration(config);
try {
Bigquery.Jobs.Insert insert = bigquery.jobs().insert(projectId,
job, content);
insert.setProjectId(projectId);
JobReference jobRef = insert.execute().getJobReference();
String jobId = jobRef.getJobId();
String status;
while (!bigquery.jobs().
get(projectId, jobId).execute().getStatus().getState().equalsIgnoreCase("DONE")) {
status = bigquery.jobs().
get(projectId, jobId).execute().getStatus().getState();
System.out.println("Status: " + status);
Thread.sleep(1000);
}
}catch (Exception e){
e.printStackTrace();
}
Check documentation for status property of job -
status.state just tells you of job completed or not
If completed ('DONE') - you still need to check how it is completed - with or without errors - you can use status.errorResult property for this
Im using Jenkins on my local in docker from jenkins official docker hub (but I even tried jenkins we have on bluemix instance).
Im writing program (test driven currently) to trigger job from java and then get job id, using
jenkins api.
Properties jenkinsProps = new Properties();
InputStream jenkinsPropsIs = Files.newInputStream(jenkinsPropsFilePath);
jenkinsProps.load(jenkinsPropsIs);
// for building url
String jenkinsServerUrl = jenkinsProps.getProperty(JenkinsPropertiesKeys.KEY_JENKINS_SERVER_URL);
String jobName = jenkinsProps.getProperty(JenkinsPropertiesKeys.KEY_JENKINS_JOB_NAME);
String jobRemoteAccessToken = jenkinsProps.getProperty(JenkinsPropertiesKeys.KEY_JENKINS_JOB_ACCESS_TOKEN);
// for headers
String jenkinsUser = jenkinsProps.getProperty(JenkinsPropertiesKeys.KEY_JENKINS_USERNAME);
String jenkinsUserApiToken = jenkinsProps.getProperty(JenkinsPropertiesKeys.KEY_JENKINS_API_TOKEN);
String jenkinsCrumb = jenkinsProps.getProperty(JenkinsPropertiesKeys.KEY_JENKINS_CSRF_CRUMB);
// build parameters
Map<String, String> params = new LinkedHashMap<>();
params.put("param1", "test1");
params.put("param2", "test2");
params.put("param3", "test3");
// Jenkins cause - to identify which process had run this job
String procID = UUID.randomUUID().toString();
params.put("cause", procID);
String url = getJenkinsBuildWithParametersUrl(jenkinsServerUrl, jobName, jobRemoteAccessToken, params);
WebRequest request = new WebRequest(); // own HttpConnection based client
// setup Jenkins crumb to avoid CSRF
request.setHeader(HEADER_NAME_JENKINS_CRUMB, jenkinsCrumb);
// user authentification (Basic + base64 encoded user:apiToken)
setupAuthenticationHeader(request, jenkinsUser, jenkinsUserApiToken);
// execute POST request
request = request.post(url);
// asserts
assertNotNull(request);
assertEquals(201, request.getResponseCode());
/* GET JOB ID */
Thread.currentThread().sleep(8000); // !!! if less then 8sec, jenkins returns old job number
request.reset();
setupAuthenticationHeader(request, jenkinsUser, jenkinsUserApiToken);
url = getJenkinsLastBuildUrl(jenkinsServerUrl, jobName);
// execute get request to /api/json
request = request.get(url);
assertTrue(request.isOK());
// get note & compare with proc id, to match job
String jenkinsJobProcId = null;
JsonObject jenkinsLastBuildJson = request.getResponseAsJson();
JsonArray jenkinsActions = jenkinsLastBuildJson.get("actions").getAsJsonArray();
for (JsonElement action : jenkinsActions) {
JsonObject actionJson = action.getAsJsonObject();
if (actionJson.get("_class").getAsString().equals("hudson.model.CauseAction")) {
JsonArray causeActionJsonArray = actionJson.get("causes").getAsJsonArray();
for (JsonElement cause : causeActionJsonArray) {
JsonObject causeJson = cause.getAsJsonObject();
if (causeJson.get("_class").getAsString().equals("hudson.model.Cause$RemoteCause")) {
jenkinsJobProcId = causeJson.get("note").getAsString();
break;
}
}
if (!jenkinsJobProcId.isEmpty()) {
break;
}
}
}
System.out.println("LastBuild prodId : " + jenkinsJobProcId);
assertEquals(procID, jenkinsJobProcId);
// get jenkins build number
int lastBuildNumber = jenkinsLastBuildJson.get("number").getAsInt();
System.out.println("LastBuild buildNumber : " + lastBuildNumber);
assertTrue(lastBuildNumber > 0);
Once i trigger job, it takes like 8 sec, to job apear in /api/json.
Do you know what can be the problem ?
How to tune it up ?
Please check if you still need a delay between the two executions.
trigger the job
curl -X POST http://${JENKINS_HOTS}:${JENKINS_PORT}/job/${JOB_NAME}/build \
--user ${USER}:${PASSWORD} \
--data-urlencode json='{"parameter": [{"name":"delay", "value":"0sec"}]}'
get job info
curl http://${JENKINS_HOTS}:${JENKINS_PORT}/job/${JOB_NAME}/api/json \
--user ${USER}:${PASSWORD}
If you still need to wait around 8 seconds, check the setting of the quiet period in the job. If it's not yet enabled, enable it and set the period to 0 seconds. This should remove the delay between the executions.
Depending on the workload off the Jenkins instance it might be necessary, even with a period of zero seconds, that you need to wait a short period.