Skip to content

Commit d252b1c

Browse files
committed
Added proxy setting when working with binary data
Proxy settings were not implemented on executeFile method - Added settings Updated example code for creating attachements to make sure the object that is having an attachment put on is active.
1 parent 17820cb commit d252b1c

2 files changed

Lines changed: 69 additions & 38 deletions

File tree

src/main/java/com/xero/api/OAuthRequestResource.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,24 +134,42 @@ public final ByteArrayInputStream executefile() throws UnsupportedOperationExcep
134134
if (this.params != null) {
135135
url.putAll(this.params);
136136
}
137+
138+
RequestConfig.Builder requestConfig = RequestConfig.custom()
139+
.setConnectTimeout(connectTimeout)
140+
.setConnectionRequestTimeout(readTimeout)
141+
.setSocketTimeout(connectTimeout);
142+
143+
//Proxy Service Setup - unable to fully test as we don't have a proxy
144+
// server to test against.
145+
if(!"".equals(config.getProxyHost()) && config.getProxyHost() != null) {
146+
int port = (int) (config.getProxyPort() == 80 && config.getProxyHttpsEnabled() ? 443 : config.getProxyPort());
147+
HttpHost proxy = new HttpHost(config.getProxyHost(), port, config.getProxyHttpsEnabled() ? "https" : "http");
148+
requestConfig.setProxy(proxy);
149+
}
137150

138151
HttpGet httpget = new HttpGet(url.toString());
139152
if (httpMethod == "GET") {
140153
this.createParameters().intercept(httpget,url);
154+
httpget.setConfig(requestConfig.build());
141155
}
142156

143157
HttpPost httppost = new HttpPost(url.toString());
144158
if (httpMethod == "POST") {
145159
httppost.setEntity(new StringEntity(this.body, "utf-8"));
146160
this.createParameters().intercept(httppost,url);
161+
httppost.setConfig(requestConfig.build());
147162
}
148163

149164
HttpPut httpput = new HttpPut(url.toString());
150165
if (httpMethod == "PUT") {
151166
httpput.setEntity(new StringEntity(this.body, "utf-8"));
152167
this.createParameters().intercept(httpput,url);
168+
httpput.setConfig(requestConfig.build());
153169
}
154170

171+
172+
155173
HttpEntity entity;
156174
try {
157175
CloseableHttpResponse response = null;
@@ -252,8 +270,7 @@ public final Map<String, String> execute() throws IOException,XeroApiException
252270
if (httpMethod == "POST") {
253271
if(logger.isInfoEnabled()){
254272
logger.info("------------------ POST: BODY -------------------");
255-
logger.info(this.body);
256-
273+
logger.info(this.body);
257274
}
258275

259276
httppost.setEntity(new StringEntity(this.body, "utf-8"));

src/main/java/com/xero/example/RequestResourceServlet.java

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -444,59 +444,73 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
444444
String newFileName = "sample2.jpg";
445445

446446
// CREATE Accounts attachment
447+
where = "Status==\"ACTIVE\"";
447448
Accounts myAccounts = accountingApi.getAccounts(ifModifiedSince, where, order);
448-
UUID accountID = myAccounts.getAccounts().get(0).getAccountID();
449-
Attachments createdAttachments = accountingApi.createAccountAttachmentByFileName(accountID, newFileName, bytes);
450-
messages.add("Attachment to Account ID: " + accountID + " attachment - ID: " + createdAttachments.getAttachments().get(0).getAttachmentID());
451-
449+
if ( myAccounts.getAccounts().size() > 0) {
450+
UUID accountID = myAccounts.getAccounts().get(0).getAccountID();
451+
Attachments createdAttachments = accountingApi.createAccountAttachmentByFileName(accountID, newFileName, bytes);
452+
messages.add("Attachment to Account ID: " + accountID + " attachment - ID: " + createdAttachments.getAttachments().get(0).getAttachmentID());
453+
}
452454
// CREATE BankTransactions attachment
453455
BankTransactions myBanktransactions = accountingApi.getBankTransactions(ifModifiedSince, where, order, page);
454-
UUID banktransactionID = myBanktransactions.getBankTransactions().get(0).getBankTransactionID();
455-
Attachments createdBanktransationAttachments = accountingApi.createBankTransactionAttachmentByFileName(banktransactionID, newFileName, bytes);
456-
messages.add("Attachment to BankTransaction ID: " + banktransactionID + " attachment - ID: " + createdBanktransationAttachments.getAttachments().get(0).getAttachmentID());
457-
456+
if ( myBanktransactions.getBankTransactions().size() > 0) {
457+
UUID banktransactionID = myBanktransactions.getBankTransactions().get(0).getBankTransactionID();
458+
Attachments createdBanktransationAttachments = accountingApi.createBankTransactionAttachmentByFileName(banktransactionID, newFileName, bytes);
459+
messages.add("Attachment to BankTransaction ID: " + banktransactionID + " attachment - ID: " + createdBanktransationAttachments.getAttachments().get(0).getAttachmentID());
460+
}
458461
// CREATE BankTransfer attachment
462+
where = null;
459463
BankTransfers myBankTransfer = accountingApi.getBankTransfers(ifModifiedSince, where, order);
460-
UUID bankTransferID = myBankTransfer.getBankTransfers().get(0).getBankTransferID();
461-
Attachments createdBankTransferAttachments = accountingApi.createBankTransferAttachmentByFileName(bankTransferID, newFileName, bytes);
462-
messages.add("Attachment to BankTransfer ID: " + bankTransferID + " attachment - ID: " + createdBankTransferAttachments.getAttachments().get(0).getAttachmentID());
463-
464+
if ( myBankTransfer.getBankTransfers().size() > 0) {
465+
UUID bankTransferID = myBankTransfer.getBankTransfers().get(0).getBankTransferID();
466+
Attachments createdBankTransferAttachments = accountingApi.createBankTransferAttachmentByFileName(bankTransferID, newFileName, bytes);
467+
messages.add("Attachment to BankTransfer ID: " + bankTransferID + " attachment - ID: " + createdBankTransferAttachments.getAttachments().get(0).getAttachmentID());
468+
}
464469
// CREATE Contacts attachment
470+
where = "Status==\"ACTIVE\"";
465471
Contacts myContacts = accountingApi.getContacts(ifModifiedSince, where, order, ids, page, includeArchived);
466-
UUID contactID = myContacts.getContacts().get(0).getContactID();
467-
Attachments createdContactAttachments = accountingApi.createContactAttachmentByFileName(contactID, newFileName, bytes);
468-
messages.add("Attachment to Contact ID: " + contactID + " attachment - ID: " + createdContactAttachments.getAttachments().get(0).getAttachmentID());
469-
472+
if ( myContacts.getContacts().size() > 0) {
473+
UUID contactID = myContacts.getContacts().get(0).getContactID();
474+
Attachments createdContactAttachments = accountingApi.createContactAttachmentByFileName(contactID, newFileName, bytes);
475+
messages.add("Attachment to Contact ID: " + contactID + " attachment - ID: " + createdContactAttachments.getAttachments().get(0).getAttachmentID());
476+
}
477+
470478
// CREATE CreditNotes attachment
471479
CreditNotes myCreditNotes = accountingApi.getCreditNotes(ifModifiedSince, where, order, page);
472-
UUID creditNoteID = myCreditNotes.getCreditNotes().get(0).getCreditNoteID();
473-
Attachments createdCreditNoteAttachments = accountingApi.createCreditNoteAttachmentByFileName(creditNoteID, newFileName, bytes);
474-
messages.add("Attachment to Credit Notes ID: " + creditNoteID + " attachment - ID: " + createdCreditNoteAttachments.getAttachments().get(0).getAttachmentID());
475-
480+
if ( myCreditNotes.getCreditNotes().size() > 0) {
481+
UUID creditNoteID = myCreditNotes.getCreditNotes().get(0).getCreditNoteID();
482+
Attachments createdCreditNoteAttachments = accountingApi.createCreditNoteAttachmentByFileName(creditNoteID, newFileName, bytes);
483+
messages.add("Attachment to Credit Notes ID: " + creditNoteID + " attachment - ID: " + createdCreditNoteAttachments.getAttachments().get(0).getAttachmentID());
484+
}
476485
// CREATE invoice attachment
477486
Invoices myInvoices = accountingApi.getInvoices(ifModifiedSince, where, order, ids, invoiceNumbers, contactIDs, statuses, page, includeArchived, createdByMyApp);
478-
UUID invoiceID = myInvoices.getInvoices().get(0).getInvoiceID();
479-
Attachments createdInvoiceAttachments = accountingApi.createInvoiceAttachmentByFileName(invoiceID, newFileName, bytes);
480-
messages.add("Attachment to Invoice ID: " + invoiceID + " attachment - ID: " + createdInvoiceAttachments.getAttachments().get(0).getAttachmentID());
481-
487+
if ( myInvoices.getInvoices().size() > 0) {
488+
UUID invoiceID = myInvoices.getInvoices().get(0).getInvoiceID();
489+
Attachments createdInvoiceAttachments = accountingApi.createInvoiceAttachmentByFileName(invoiceID, newFileName, bytes);
490+
messages.add("Attachment to Invoice ID: " + invoiceID + " attachment - ID: " + createdInvoiceAttachments.getAttachments().get(0).getAttachmentID());
491+
}
482492
// CREATE ManualJournals attachment
483493
ManualJournals myManualJournals = accountingApi.getManualJournals(ifModifiedSince, where, order, page);
484-
UUID manualJournalID = myManualJournals.getManualJournals().get(0).getManualJournalID();
485-
Attachments createdManualJournalAttachments = accountingApi.createManualJournalAttachmentByFileName(manualJournalID, newFileName, bytes);
486-
messages.add("Attachment to Manual Journal ID: " + manualJournalID + " attachment - ID: " + createdManualJournalAttachments.getAttachments().get(0).getAttachmentID());
487-
494+
if ( myManualJournals.getManualJournals().size() > 0) {
495+
UUID manualJournalID = myManualJournals.getManualJournals().get(0).getManualJournalID();
496+
Attachments createdManualJournalAttachments = accountingApi.createManualJournalAttachmentByFileName(manualJournalID, newFileName, bytes);
497+
messages.add("Attachment to Manual Journal ID: " + manualJournalID + " attachment - ID: " + createdManualJournalAttachments.getAttachments().get(0).getAttachmentID());
498+
}
488499
// CREATE Receipts attachment
489500
Receipts myReceipts = accountingApi.getReceipts(ifModifiedSince, where, order);
490-
UUID receiptID = myReceipts.getReceipts().get(0).getReceiptID();
491-
Attachments createdReceiptsAttachments = accountingApi.createReceiptAttachmentByFileName(receiptID, newFileName, bytes);
492-
messages.add("Attachment to Receipt ID: " + receiptID + " attachment - ID: " + createdReceiptsAttachments.getAttachments().get(0).getAttachmentID());
493-
501+
if ( myReceipts.getReceipts().size() > 0) {
502+
UUID receiptID = myReceipts.getReceipts().get(0).getReceiptID();
503+
Attachments createdReceiptsAttachments = accountingApi.createReceiptAttachmentByFileName(receiptID, newFileName, bytes);
504+
messages.add("Attachment to Receipt ID: " + receiptID + " attachment - ID: " + createdReceiptsAttachments.getAttachments().get(0).getAttachmentID());
505+
}
494506
// CREATE Repeating Invoices attachment
495507
RepeatingInvoices myRepeatingInvoices = accountingApi.getRepeatingInvoices(where, order);
496-
UUID repeatingInvoiceID = myRepeatingInvoices.getRepeatingInvoices().get(0).getRepeatingInvoiceID();
497-
Attachments createdRepeatingInvoiceAttachments = accountingApi.createRepeatingInvoiceAttachmentByFileName(repeatingInvoiceID, newFileName, bytes);
498-
messages.add("Attachment to Repeating Invoices ID: " + repeatingInvoiceID + " attachment - ID: " + createdRepeatingInvoiceAttachments.getAttachments().get(0).getAttachmentID());
499-
508+
if ( myRepeatingInvoices.getRepeatingInvoices().size() > 0) {
509+
UUID repeatingInvoiceID = myRepeatingInvoices.getRepeatingInvoices().get(0).getRepeatingInvoiceID();
510+
Attachments createdRepeatingInvoiceAttachments = accountingApi.createRepeatingInvoiceAttachmentByFileName(repeatingInvoiceID, newFileName, bytes);
511+
messages.add("Attachment to Repeating Invoices ID: " + repeatingInvoiceID + " attachment - ID: " + createdRepeatingInvoiceAttachments.getAttachments().get(0).getAttachmentID());
512+
}
513+
where = null;
500514
} else if(object.equals("Assets")) {
501515
/* Asset */
502516
// Create Asset

0 commit comments

Comments
 (0)