How to create shared link in box using java sdk - java

Can any one help me on how to create shared link in BOX using java SDK. I am using below code:-
BoxFile file = new BoxFile(api, ID);
BoxSharedLink.Permissions permissions = new BoxSharedLink.Permissions();
permissions.setCanDownload(true);
permissions.setCanPreview(true);
Date unshareDate = new Date();
BoxSharedLink sharedLink = file.createSharedLink(
BoxSharedLink.Access.OPEN, unshareDate, permissions);
Getting error :-
The API returned the error code: 400
{"type":"error","status":400,"code":"bad_request","context_info":{"errors":[{"reason":"invalid_parameter","name":"unshared_at","message":"Invalid value '1471842735'."}]},"help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Bad Request","request_id":"208420399157ba89af5e170"}

private static BoxSharedLink createSharedLink(BoxAPIConnection api, String fileId) {
BoxFile file = new BoxFile(api, fileId);
BoxSharedLink.Permissions permissions = new BoxSharedLink.Permissions();
permissions.setCanDownload(true);
permissions.setCanPreview(true);
Date date = new Date();
Calendar unshareAt = Calendar.getInstance();
unshareAt.setTime(date);
unshareAt.add(Calendar.DATE, 14);
BoxSharedLink sharedLink = file.createSharedLink(BoxSharedLink.Access.COMPANY, unshareAt.getTime(), permissions);
logger.info("shared link: " + sharedLink.getURL());
return sharedLink;
}

I just passed "null" in place of unsharedDate..I am able to get a shared link.
BoxSharedLink sharedLink = file.createSharedLink(
BoxSharedLink.Access.OPEN, null, permissions);
I am not sure what null value means. I am guessing there is no unsharedDate set if you pass null. couldn't find any api documentation for this.

Related

How to get Latest Log Stream Name Using Java

How do we get the latest log stream name from a given log group using java code.
I have tried many possibilities and still not able to retrieve it.
AWSLogs awsLogs = AWSLogsClientBuilder
.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(Regions.EU_WEST_1)
.build();
DescribeLogStreamsRequest describeLogStreamsRequest = new DescribeLogStreamsRequest()
.withLogGroupName("/aws/lambda/a250348-ecndesc-conversion-lambda-reci-euw1-qe");
describeLogStreamsResult = awsLogs.describeLogStreams(describeLogStreamsRequest);
System.out.println(describeLogStreamsResult.getLogStreams());
for(int i =0; i<describeLogStreamsResult.getLogStreams().size(); i++) {
logStream = describeLogStreamsResult.getLogStreams().get(i).getLastEventTimestamp();
Date date = new Date(logStream);
dateList.add(date);
}
latestDate = Collections.max(dateList);
System.out.println(latestDate);
System.out.println(dateList.indexOf(latestDate));
logStream1 = describeLogStreamsResult.getLogStreams().get(dateList.indexOf(latestDate)).getLogStreamName();
System.out.println(logStream1);
Try to order your results:
DescribeLogStreamsRequest describeLogStreamsRequest = new DescribeLogStreamsRequest()
.withLogGroupName("/aws/lambda/a250348-ecndesc-conversion-lambda-reci-euw1-qe")
.withOrderBy(com.amazonaws.services.logs.model.OrderBy.LastEventTime)
.withDescending(true);
This will return first the log stream with the latest event time (and not necessarily the last created). This is how results are presented in AWS console.

How to create a CNAME Record with AWS SDK

I am able to create a CNAME record through the AWS console no problem see picture above. But when I try to do the same thing programmatically with the AWS Route53 SDK I keep getting an
com.amazonaws.services.route53.model.InvalidInputException: Invalid
request (Service: AmazonRoute53; Status Code: 400; Error Code:
InvalidInput; Request ID: 7fd7b5d1-5ea8-11e8-a252-7f474094c446)
Here's my attempted code
private static final String QA_HOSTED_ZONE_ID = "UUIDFromConsole";
GetHostedZoneRequest getHostedZoneRequest = new GetHostedZoneRequest(HOSTED_ZONE_ID);
GetHostedZoneResult result = route53Client.getHostedZone(getHostedZoneRequest);
ResourceRecordSet resourceRecordSet = new
ResourceRecordSet("DN4TheRecordSet", RRType.CNAME);
GeoLocation geoLocation = new GeoLocation();
geoLocation.setContinentCode("NA");
geoLocation.setSubdivisionCode("NY");
resourceRecordSet.setGeoLocation(geoLocation);
resourceRecordSet.setTTL(50L);
resourceRecordSet.setGeoLocation(geoLocation);
resourceRecordSet.setSetIdentifier("uniqueid-statecode");Change addStateChange = new Change(ChangeAction.CREATE, resourceRecordSet);
ChangeBatch changeBatch = new ChangeBatch(Arrays.asList(addStateChange));
ChangeResourceRecordSetsRequest changeResourceRecordSetsRequest = new ChangeResourceRecordSetsRequest().withHostedZoneId(HOSTED_ZONE_ID).withChangeBatch(changeBatch);
route53Client.changeResourceRecordSets(changeResourceRecordSetsRequest);
I am also not able to configure the value for the CNAME, meaning where I want to redirect my traffic after it hits the DN4TheRecordSet.This is probably the cause of the invalid error I am receiving in the response.
Please help 🤕
Found the issue.
The InvalidInputException was eluding to the fact of a missing ResourceRecord which corresponds to the value field in the picture above.
Here's the solution above with the addition of a ResourceRecord to the ResourceRecordSet
private static final String QA_HOSTED_ZONE_ID = "UUIDFromConsole";
private static final String REDIRECT_DNS = "DNS to want the cname to route to";
GetHostedZoneRequest getHostedZoneRequest = new GetHostedZoneRequest(HOSTED_ZONE_ID);
GetHostedZoneResult result = route53Client.getHostedZone(getHostedZoneRequest); ResourceRecordSet resourceRecordSet = new ResourceRecordSet("DN4TheRecordSet", RRType.CNAME);
GeoLocation geoLocation = new GeoLocation();
geoLocation.setContinentCode("NA");
geoLocation.setSubdivisionCode("NY");
resourceRecordSet.setGeoLocation(geoLocation);
resourceRecordSet.setTTL(50L);
resourceRecordSet.setGeoLocation(geoLocation);
resourceRecordSet.setSetIdentifier("uniqueid-statecode");
resourceRecordSet.setResourceRecords(Arrays.asList(new ResourceRecord(REDIRECT_DNS))); // This line being missing cause the exception
Change addStateChange = new Change(ChangeAction.CREATE, resourceRecordSet);
ChangeBatch changeBatch = new ChangeBatch(Arrays.asList(addStateChange));
ChangeResourceRecordSetsRequest changeResourceRecordSetsRequest = new ChangeResourceRecordSetsRequest().withHostedZoneId(HOSTED_ZONE_ID).withChangeBatch(changeBatch);
route53Client.changeResourceRecordSets(changeResourceRecordSetsRequest);
See Geolocation Syntax section in this page for example of the payload the sdk creates and sends for your request.

Date insertion in Google sheet appending ' in cell

To write the data into the Google spreadsheet I am using following code.
private static void writeValuesInSpreedSheet(Sheets service, String spreadsheetId, int sheetSize) throws IOException {
String range = "A"+(sheetSize+1)+":K"+(sheetSize+1);
List<List<Object>> newData = new ArrayList<>();
List<Object> rowValues = new ArrayList();
rowValues.add(getCurentDateInESTFormat());
rowValues.add("2");
rowValues.add("3");
rowValues.add("4");
rowValues.add("5");
rowValues.add("6");
rowValues.add("7");
rowValues.add("8");
rowValues.add("9");
rowValues.add("10");
rowValues.add("11");
/* List<Object> rowValues1 = new ArrayList();
rowValues1.add("1");
rowValues1.add("2");*/
newData.add(rowValues);
//newData.add(rowValues1);
// response.setValues(newData);
ValueRange oRange = new ValueRange();
oRange.setRange(range); // I NEED THE NUMBER OF THE LAST ROW
oRange.setValues(newData);
List<ValueRange> oList = new ArrayList<>();
oList.add(oRange);
BatchUpdateValuesRequest oRequest = new BatchUpdateValuesRequest();
oRequest.setValueInputOption("RAW");
oRequest.setData(oList);
BatchUpdateValuesResponse oResp1 = service.spreadsheets().values().batchUpdate(spreadsheetId, oRequest).execute();
System.out.println("Response Values " +oResp1.values());
}
private static Object getCurentDateInESTFormat() {
SimpleDateFormat sdfAmerica = new SimpleDateFormat("MM/dd/YYYY");
sdfAmerica.setTimeZone(TimeZone.getTimeZone("America/New_York"));
String sDateInAmerica = sdfAmerica.format(new Date());
return sDateInAmerica;
}
In sheet we have defined the date and currency type of respective column.
I am able to write the data but eventually its prepending ' in the data for example - '09/04/2016
Because of this we are not able to open it into date format. I have attached one screen shot as well.
We are using Google Sheets API V4.
I am asking this question because i did not find any link/solution related to it.
When using the ValueInputOption RAW you need to pass dates in the serial number format. The Apache POI library provides a getExcelDate method that will handle most of this for you, but you'll need to add a day to account for the difference in the epoch used.

Issue while adding attachment to QC Test Case using Com4j API

I am trying to add attachment to QC Test LAB Test Case run from my Java code using Com4J API. I was able to create a successful run, however while adding attachments below code is throwing invalid parameter for "IAttachment attach = attachfac.addItem(null).queryInterface(IAttachment.class);". In this case additem is expecting Java Item Object. I also tried to pass addItem(""), but then attach.Type(1) is failing with reason:- Attachment Type cannot be changed. Could anyone please help me with this:
IBaseFactory obj2 = testset.tsTestFactory().queryInterface(IBaseFactory.class);
IList tstestlist = obj2.newList("");
for(Com4jObject obj3:tstestlist){
ITSTest tstest = obj3.queryInterface(ITSTest.class);
if(tstest.name().contentEquals("[1]TC1")){
System.out.println("TC found");
IRunFactory runfactory = tstest.runFactory().queryInterface(IRunFactory.class);
IRun run=runfactory.addItem("RunNew").queryInterface(IRun.class);
run.status("Passed");
IAttachmentFactory attachfac = run.attachments().queryInterface(IAttachmentFactory.class);
IAttachment attach = attachfac.addItem("").queryInterface(IAttachment.class);
attach.type(1);
attach.fileName("Path to File TC1");
attach.post();
run.post();.
String fileName = new File(Attachment).getName();
String folderName = new File(Attachment).getParent();
try{
IAttachmentFactory attachfac = tsteststeps.attachments().queryInterface(IAttachmentFactory.class);
IAttachment attach = attachfac.addItem(fileName).queryInterface(IAttachment.class);
IExtendedStorage extAttach = attach.attachmentStorage().queryInterface(IExtendedStorage.class);
extAttach.clientPath(folderName);
extAttach.save(fileName, true);
attach.description(Actual);
attach.post();
attach.refresh();
}catch(Exception e) {
System.out.println("QC Exceptione : "+e.getMessage());
}

How do I get all the calendar entries for a particular time range using Google Calendar API

I want to view events over specific time range for a specific calendar, but am having trouble using the API, It is a generic API, and it reminds me of using the DOM. The problem is that it seems difficult to work with because much of the information is in generic base classes.
How do I get the events for a calendar using Groovy or Java?
Does anybody have an example of passing credentials using curl?
Example code would be appreciated.
This document has examples for most of the common use cases. For example, here's the code for retrieving events for a specific time range
URL feedUrl = new URL("http://www.google.com/calendar/feeds/default/private/full");
CalendarQuery myQuery = new CalendarQuery(feedUrl);
myQuery.setMinimumStartTime(DateTime.parseDateTime("2006-03-16T00:00:00"));
myQuery.setMaximumStartTime(DateTime.parseDateTime("2006-03-24T23:59:59"));
CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
myService.setUserCredentials("jo#gmail.com", "mypassword");
// Send the request and receive the response:
CalendarEventFeed resultFeed = myService.query(myQuery, Feed.class);
You could make this a bit Groovier, using something like:
def myQuery = new CalendarQuery("http://www.google.com/calendar/feeds/default/private/full".toURL()).with {
minimumStartTime = DateTime.parseDateTime("2006-03-16T00:00:00");
maximumStartTime = DateTime.parseDateTime("2006-03-24T23:59:59");
it
}
def myService = new CalendarService("exampleCo-exampleApp-1");
myService.setUserCredentials("jo#gmail.com", "mypassword");
// Send the request and receive the response:
def resultFeed = myService.query(myQuery, Feed);
If you do not need to alter the calendar, you only need to get the calendars private feed url, and you can use something like this (taken from the http://eu.gr8conf.org/agenda page). It uses the ICal4J library.
def url = "http://www.google.com/calendar/ical/_SOME_URL_/basic.ics".toURL()
def cal = Calendars.load(url)
def result = cal.components.sort { it.startDate.date }.collect {
def e = new Expando()
e.startDate = it.startDate.date
e.endDate = it.endDate.date
e.title = it.summary.value
if (it.location) {
e.presentation = Presentation.findByName(it.location.value, [fetch:"join"])
}
e.toString = {
"$startDate: $title"
}
return e
}
result
Happy hacking.

Categories