Get file size of an URL without download - codenameone

To get the file size of an URL without download, I wrote:
public static long getFileSizeWithoutDownload(String url) {
ConnectionRequest cr = new GZConnectionRequest();
cr.setUrl(url);
cr.setPost(false);
NetworkManager.getInstance().addProgressListener((NetworkEvent evt) -> {
if (cr == evt.getConnectionRequest() && evt.getLength() > 0) {
cr.kill();
}
});
NetworkManager.getInstance().addToQueueAndWait(cr);
return cr.getContentLength();
}
It seems to work on Simulator, Android and iOS with a testing URL of my Spring Boot server.
Anyway, I consider this code as a workaround, as I couldn't find an API that directly gives me the file size without starting the download first. Starting the download and then killing it works, but maybe there may be a better way to get the same result. By the way, the condition && evt.getLength() > 0 may never be satisfied in some cases (depending on the headers received), so it would be better to read only the headers, in which "Content-Length" may be present or absent.
So, my question is if, with Codename One, there is a way to download only the response headers, without starting the download. Thank you.

Using the HTTP head request should give you the content length header that you can then use to get the size of the file without triggering a download. Your code might not follow through on the download but it does physically happen so a head request would be superior.
Unfortunately while there's a nice wrapper to head in Rest. This wrapper isn't very useful since there's no API to query response headers. That would make sense as an enhancement. You would need to derive ConnectionRequest and read the server response headers to get the content length.

Thank you Shai, your answer https://stackoverflow.com/a/62124902/1277576 led me in the right direction. cr.setHttpMethod("HEAD"); simplifies the code and prevents the download from starting:
public static long getFileSizeWithoutDownload(String url) {
ConnectionRequest cr = new GZConnectionRequest();
cr.setUrl(url);
cr.setHttpMethod("HEAD");
cr.setPost(false);
NetworkManager.getInstance().addToQueueAndWait(cr);
return cr.getContentLength();
}
However, as you wrote, I can override ConnectionRequest for a more precise control of the headers. This other method performs the same function as the previous one, but it also guarantees me that the server supports partial downloads. In fact, if the server does not support partial downloads, the information about the content length would be useless for my purposes:
/**
* Returns -2 if the server doesn't accept partial downloads, -1 if the
* content length is unknow, a value greater than 0 if the Content-Length is
* known
*
* #param url
* #return must be interpreted as a boolean value: if greater than zero than
* partial downloads are supported (the returned value is the Content-Length),
* otherwise they are not supported.
*/
public static long getFileSizeWithoutDownload(String url) {
// documentation about the headers: https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests
Wrapper<Long> result = new Wrapper<>(0l);
ConnectionRequest cr = new GZConnectionRequest() {
#Override
protected void readHeaders(Object connection) throws IOException {
String acceptRanges = getHeader(connection, "Accept-Ranges");
if (acceptRanges == null || !acceptRanges.equals("bytes")) {
Log.p("The partial downloads of " + url + " are not supported.", Log.WARNING);
result.set(-2l);
} else {
String contentLength = getHeader(connection, "Content-Length");
if (contentLength != null) {
result.set(Long.parseLong(contentLength));
} else {
Log.p("The Content-Length of " + url + " is unknown.", Log.WARNING);
result.set(-1l);
}
}
}
};
cr.setUrl(url);
cr.setHttpMethod("HEAD");
cr.setPost(false);
NetworkManager.getInstance().addToQueueAndWait(cr);
return result.get();
}
The readHeaders and getHeader methods are implementation dependent. I have verified that they work as desired on Simulator, Android and iOS.
Lastly, the Wrapper class is so implemented:
/**
* Generic object wrapper, as workaround for the issue "Local variables
* referenced from a lambda expression must be final or effectively final".
*/
public class Wrapper<T> {
private T object;
public Wrapper(T obj) {
this.object = obj;
}
public T get() {
return object;
}
public void set(T obj) {
this.object = obj;
}
}
I hope this detailed answer will help those who need to read HTTP headers with Codename One.

Related

How to retrieve all instances on the JHipster API for entities

When calling the generated api when using a paginator, is there any way i can call the generated REST-api to retrieve ALL instances of an object, insted of only the first 20,30,40 etc?
I find that since i am using pagination for my entity-creation and management, when i want to utilize these entities in other views (self created), then the API does not provide all the instances when calling the entity.query() in angular/js.
Is this a limitation to JHipster, or can i call the REST-API in any other way supplying info to discard the paginator?
You can modify existing rest controller for that entity. Here is an example with a Center entity.
I return all centers if there is no value for offset and limit.
#RequestMapping(value = "/centers",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
#Timed
public ResponseEntity<List<Center>> getAll(#RequestParam(value = "page" , required = false) Integer offset,
#RequestParam(value = "per_page", required = false) Integer limit)
throws URISyntaxException {
if(offset == null && limit == null) {
return new ResponseEntity<List<Center>>(centerRepository.findAll(), HttpStatus.OK);
} else {
Page<Center> page = centerRepository.findAll(PaginationUtil.generatePageRequest(offset, limit));
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/centers", offset, limit);
return new ResponseEntity<List<Center>>(page.getContent(), headers, HttpStatus.OK);
}
}
Then in angular, you just have to call Center.query(); without params.
It's an old question but for anyone who's looking for easy solution. You need to override default PageableHandlerMethodArgumnetResolver bean:
#Configuration
public class CustomWebConfigurer implements WebMvcConfigurer {
#Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver();
resolver.setFallbackPageable(Pageable.unpaged());
argumentResolvers.add(resolver);
}
}

Salesforce: Trying to deliver a CSV from a scheduled job

I read where someone was able to do this, but I'm having a hard time getting it to work.
Basically, I'm scheduling an HTTP callout to a page that has a controller that builds a CSV and emails it to a recipient.
The Scheduled class:
global class ReportExporter implements System.Schedulable {
global void execute(SchedulableContext sc) {
getmailReportOutput ex = new getmailReportOutput();
ex.exportCSV();
}
}
The getEmailReportOutput class:
public class getmailReportOutput{
public Static String strSessionID;
public getmailReportOutput() {
}
public void exportCSV() {
makeReportRequest();
}
#future (callout=true)
public Static void makeReportRequest() {
strHost ='c.cs4.visual.force.com';
strSessionID = UserInfo.getSessionId();
String requestUrl = 'https://' + strHost + '/apex/TestSendReport#';
HttpRequest req = new HttpRequest();
req.setEndpoint(requestUrl);
req.setMethod('GET');
req.setHeader('Cookie','sid=' + strSessionID );
String output = new Http().send(req).getBody();
System.debug('HTTP RESPONSE RETURNED: ' + output);
}
}
The getEmailReportOutput class does an HTTP Callout to a VF page: I make sure to send the sessionID with the request:
And the "TestSendReport" is just a simple callout to a controller:
<apex:page controller="Exporter" action="{!runrpt}">
</apex:page>
...And the controller is calling the report content:
public class Exporter {
public static Boolean isTest;
public static String strEmailAddr;
public void runrpt() {
executeRpt();
}
#future
public static void executeRpt() {
System.debug('CALLING REPORT EXPORTER...');
String ReportName__c = '00OP0000000Jp3N';
String strEmailAddr = 'myname#email.com';
ApexPages.PageReference report = new ApexPages.PageReference( '/' + RptName__c + '?csv=1');
Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
attachment.setFileName('report.csv');
attachment.setBody(report.getContent());
attachment.setContentType('text/csv');
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } );
message.setSubject('Report');
message.setPlainTextBody('The report is attached.');
message.setToAddresses( new String[] { strEmailAddr } );
Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } );
}
}
...Any ideas? The debug logs show all is well, but nothing is received. I know this is a wall of code, but it seems to be what people recommend to accomplish the task - I just can't see anything wrong.
I don't see anything obviously missing here :/
Just to be safe - getEmailReportOutput & getmailReportOutput are the same class (typo error in the post, not in your actual code)?
This looks like jumping a lot of hops, do I read it correctly that it's scheduled class -> REST callout -> VF page with action -> #future -> send an email? Geez, a lot can go wrong here ;) I've read somewhere that SF will keep some kind of reference counter and calling out to same instance might block you from using page.getContent...
Can you see the report body System.debug(report.getContent().toString());? Can you try saving this email as task for your own user or under a sample Account for example (setSaveAsActivity())?
As blatant plug as it is - I've used different path to solve similar requirement. Check out https://salesforce.stackexchange.com/questions/4303/scheduled-reports-as-attachment and see if you can get it to work?

GWT pass Objectify Cursor from Server to Client with RequestFactory and show more pages in DataGrid

I am brand new to cursors and have the following method:
public List<Venue> findPage(Subject subject, Objectify ofy, int pageSize) {
final Business business = (Business) subject.getSession().getAttribute(BUSINESS_ATTRIBUTE);
final Query<Venue> query = ofy.query(Venue.class).filter(BUSINESS_ATTRIBUTE, business);
final String encodedCursor = (String) subject.getSession().getAttribute(VENUE_CURSOR_CONDITION);
if (encodedCursor != null) {
query.startCursor(Cursor.fromWebSafeString(encodedCursor));
}
final QueryResultIterator<Key<Venue>> iterator = query.fetchKeys().iterator();
final List<Key<Venue>> data = new ArrayList<Key<Venue>>(pageSize);
boolean more = false;
for (int i = 0; i < pageSize && (more = iterator.hasNext()); i++) {
data.add(iterator.next());
}
subject.getSession().setAttribute(VENUE_CURSOR_CONDITION, iterator.getCursor().toWebSafeString());
return get(ofy, data);
}
where get is the following method
public <T> List<T> get(Objectify ofy, List<Key<T>> keys) {
if (keys == null) {
return null;
}
final Map<Key<T>, T> map = ofy.get(keys);
final List<T> list = new ArrayList<T>();
for (T t : map.values()) {
list.add(t);
}
return list;
}
Now, here's what I'm doing currently - which is a little hackish. I am using Apache Shiro to track User's sessions - in the user's session I am keeping the last cursor that was used - if the user navigates away from the page i set the cursor to false (obviously - this is very, very brittle as it requires every method to have a code to invalidate the last cursor).
Now, here's where I'm getting caught. I made a simple HasCursor interface, and was going to pass back a List with Cursor in it to the client side - the only problem is RequestFactory can only handle passing certain types from server to client - this Custom type isn't one of them. So, right now with GWT RequestFactory - is there a good way to pass an Objectify Cursor from the Server to the Client?
Also - another issue I'm running into - if I populate the DataGrid with this data it shows the data fine in the grid - but that's it - the pager says 1-25 out of 25. So, what I'm wondering is, how do you actually tell the DataGrid that there is more data available on the server. I'm a little confused as it seems there isn't a whole lot of documentation / examples available for this.
Thank you very much
for anybody else that runs into this problem i found out the answer to one part of it - i forgot all about ValueProxies that allow you to map standard java beans to return to the client side from the server with RequestFactory.
Unfortunately, you can't use Generics with ValueProxies so I had to create a ListCursor wrapper for each individual type.
Here's the proxy:
#ProxyFor(value = VenueListCursorWrapper.class)
public interface VenueListCursorWrapperProxy extends ValueProxy {
List<VenueProxy> getVenues();
String getCursor();
boolean isMoreAvailable();
void setVenues(List<VenueProxy> venues);
void setCursor(String cursor);
void setMoreAvailable(boolean moreAvailable);
}
and here's the updated method on the server:
public VenueListCursorWrapper findPage(Subject subject, Objectify ofy, int pageSize, String cursor) {
final Business business = (Business) subject.getSession().getAttribute(BUSINESS_ATTRIBUTE);
final Query<Venue> query = ofy.query(Venue.class).filter(BUSINESS_ATTRIBUTE, business);
if (cursor != null) {
query.startCursor(Cursor.fromWebSafeString(cursor));
}
final QueryResultIterator<Key<Venue>> iterator = query.fetchKeys().iterator();
final List<Key<Venue>> data = new ArrayList<Key<Venue>>(pageSize);
boolean more = false;
for (int i = 0; i < pageSize && (more = iterator.hasNext()); i++) {
data.add(iterator.next());
}
return new VenueListCursorWrapper(get(ofy, data), iterator.getCursor().toWebSafeString(), more);
}
So - that's that part of it - the one thing I still haven't quite figured out how to do is get the pager set up correctly - I would like to show the total count of items in the pager to allow you to page through it but I'm not sure exactly how to do this yet - if I find a solution I will post it
You can convert Cursor to a serializable string - actually you already use this: iterator.getCursor().toWebSafeString().
Just return the serialized cursor (= a String) to the client together with all other list data and on the next request provide it as a parameter.
Optonaly use it in fragment identifier, aka GWT history token, so that user can actually bookmark the particular page in your list (depends on the use case, makes sense if list data does not change much).

Restlet GWT in a Google Gadget

I am developing a GWT app for the Google Apps marketplace. I am using AppEngine with Restlet on the server side. Client side I use the GWT edition of Restlet. This is a great combination. I have my domain objects shared between client and server and as such no need for DTO's or proxies and so on. On the client side I can simply call Restlet resources :
CustomerResourceProxy customerResource = GWT.create(CustomerResourceProxy.class);
customerResource.getClientResource().setReference("/customer");
customerResource.retrieve(new Result<Customer>() { .... }
No need to parse the underlying XML or use JSNI to interpret incoming JSON.
BUT... part of the app is a GMAIL contextual gadget, and I cannot simply use the above code because all communication between a Gadget and the server must pass through GadgetsIO makeRequest.
So... just for the gadget, I will have to make the effort of parsing the XML or using JSNI to interpret the incoming JSON.
Is it überhaupt possible to hack the Restlet GWT client to pass all communication via GadgetsIO and what would it take ? Any pointers very welcome !
K.
I managed to get Restlet resources to work within a Gadget using GWT by making some changes to the Restlet GWT edition :
In GwtClientCall I replaced the standard GWT requestbuilder by the GadgetRequestBuilder (which will IoProvider.makeRequest), like this :
public GwtClientCall(GwtHttpClientHelper helper, String method, String requestUri, boolean hasEntity) {
super(helper, method, requestUri);
Reference requestRef = new Reference(requestUri);
if (requestRef.isRelative() || requestRef.getScheme().startsWith("http")) {
this.requestBuilder = new GadgetsRequestBuilder(method, requestUri);
this.requestBuilder.setTimeoutMillis(getHelper().getSocketConnectTimeoutMs());
this.responseHeadersAdded = false;
} else {
throw new IllegalArgumentException("Only HTTP or HTTPS resource URIs are allowed here");
}
}
In the gadgetsrequestbuilder, I had to make some changes so it would pass the headers in the request :
private GadgetsRequest doSend(String requestData, final RequestCallback callback) throws RequestException {
final RequestOptions options = RequestOptions.newInstance();
options.setMethodType(methodType);
if (requestData != null && requestData.length() > 0) {
options.setPostData(requestData);
}
options.setAuthorizationType(AuthorizationType.SIGNED);
options.setContentType(ContentType.DOM);
setHeaders(options);
final GadgetsRequest gadgetsRequest = new GadgetsRequest(getTimeoutMillis(), callback);
gadgetsRequest.setPending(true);
IoProvider.get().makeRequest(getUrl(), new ResponseReceivedHandler<Object>() {
public void onResponseReceived(ResponseReceivedEvent<Object> event) {
gadgetsRequest.fireOnResponseReceived(event, callback);
}
}, options);
return gadgetsRequest;
}
the gadget container by default strips the response headers, so i manually add the MediaType.APPLICATION_JAVA_OBJECT_GWT
#Override
public Series<org.restlet.client.engine.header.Header> getResponseHeaders() {
final Series<org.restlet.client.engine.header.Header> result = super.getResponseHeaders();
if (!this.responseHeadersAdded && (getResponse() != null)) {
Header[] headers = getResponse().getHeaders();
for (int i = 0; i < headers.length; i++) {
if (headers[i] != null) {
result.add(headers[i].getName(), headers[i].getValue());
}
}
result.add(HeaderConstants.HEADER_CONTENT_TYPE, MediaType.APPLICATION_JAVA_OBJECT_GWT.toString());
this.responseHeadersAdded = true;
}
return result;
}
A lot of dialogboxes for debugging later, it works :-)

NetConnect fails silently in Flash when called from SilverLight

This is a complex question, because there are a lot of moving parts. My apologies in advance.
I'm trying to write a Silverlight control that hosts a Flash camera and microphone (since Silverlight doesn't support these things natively, worse luck). I've written a short little Flex application ("WLocalWebCam.swf") which handles the camera, and exposes two external methods: connect(uri:String, streamName:String), and disconnect(). I can call these successfully through JavaScript as follows (simplified to remove error handling, etc.):
function connectWebCam(webCamID, rtmpUri, streamName) {
var flashCam = getWebCam(webCamID);
flashCam.Connect(rtmpUri, streamName);
}
function disconnectWebCam(webCamID) {
var flashCam = getWebCam(webCamID);
flashCam.Disconnect();
}
function getWebCam(id) {
return document.getElementById(id);
}
When I call these functions from another JavaScript source (e.g., a button click handler), the web cam connects correctly up to the RTMP server (I'm using Wowza). However, when I call exactly these same functions on the same page from Silverlight, the Flash camera fails to connect to the RTMP server.
/// <summary>
/// Connect() invokes the connectWebCam() JavaScript function contained in the WebCam.js file on the website.
/// The connectWebCam() method in turn calls the Connect() method on the contained Flash web cam object.
/// </summary>
public void Connect()
{
ScriptObject connectWebCam = (ScriptObject)HtmlPage.Window.GetProperty("connectWebCam");
connectWebCam.InvokeSelf(CameraID, RtmpUri.ToString(), CameraID);
}
However, it fails in an interesting fashion. The ActionScript connect() method gets called, it successfully calls getConnection(), but the handleNetStatus event handler method never gets called, and the Wowza server never sees an attempt to connect.
Here's the ActionScript code I'm using, this time with the debugging bits left in.
public function connect(uri:String, name:String):void
{
rtmpUri = uri;
streamName = name;
logMessage("Beginning attempt to open connection; rtmpUri=" + rtmpUri + "; streamName = " + streamName);
logMessage("Retrieving camera.");
cam = Camera.getCamera();
if( cam != null )
{
logMessage("Retrieved camera.");
cam.setMode( 320, 240, 20 );
cam.setQuality( 0,0 );
}
else
{
logMessage("Unable to retrieve camera instance.");
}
logMessage("Retrieving microphone.");
mic = new Microphone();
if (mic == null)
{
logMessage("Unable to retrieve microphone instance.");
}
logMessage("Retrieving NetConnection.");
chatNC = getConnection();
}
private function getConnection(): NetConnection
{
var nc: NetConnection = new NetConnection();
if (nc != null)
{
logMessage("Retrieved NetConnection().");
nc.client = this;
nc.objectEncoding = ObjectEncoding.AMF0;
nc.addEventListener( NetStatusEvent.NET_STATUS, this.handleNetStatus );
logMessage("Connecting to " + rtmpUri);
nc.connect(rtmpUri); // <-- We get successfully to this point.
}
else
{
logMessage("Unable to retrieve new NetConnection()");
}
return nc;
}
private function handleNetStatus( event:NetStatusEvent ):void
{
logMessage("[SYSTEM MESSAGE] net status " + event.info.code + " type " + event.type); // <-- We never get this far. This bit never gets called.
switch( event.info.code )
{
case "NetConnection.Connect.Success":
publishVideoStream();
break;
default:
Alert.show("Error connecting: " + event.info.code, "Error Connecting");
break;
}
}
This has got me seriously scratching my head.
Does anyone know why the exact same ActionScript would behave differently if called from Silverlight vs. being called from JavaScript? Any suggestions on troubleshooting it?
Sigh. Never mind. Turns out it makes a difference if you try to connect to http://localhost/videochat instead of rtmp://localhost/videochat. It all works as expected when you give it the right parameters. I must have looked at that code a hundred times before I spotted what I did wrong.

Resources