JAVA Question: getResourceAsStream()

452 views
Skip to first unread message

AJ Mercer

unread,
Nov 29, 2016, 2:50:05 AM11/29/16
to lu...@googlegroups.com
in CFML, how can I get to getResourceAsStream()?

cl = createObject("java", "java.lang.Class");

dump(cl);

cl = createObject("java", "java.lang.ClassLoader");

dump(cl);


both output

java.io.InputStreamgetResourceAsStream(java.lang.String)


​But when I do this I get an error

myR = cl.getResourceAsStream(variables.json);

dump(myR);

Inline images 1

Inline images 2


​I need to load a file and kept it's mime type​

from Java class that I am passing file to
JsonObjectParser parser = new JsonObjectParser(jsonFactory);
    GenericJson fileContents = parser.parseAndClose(
        credentialStream, OAuth2Utils.UTF_8, GenericJson.class);
    String fileType = (StringfileContents.get("type");
    if (fileType == null) {
      throw new IOException("Error reading credentials from stream, 'type' field not specified.");
    }

--

Joseph Gooch

unread,
Nov 29, 2016, 8:30:12 PM11/29/16
to lu...@googlegroups.com
I don't think that's quite what you want to do.

getResource would load a file from a classpath or jar... not from a variable.  If you want to pull a file in as a stream you want something else.

You'd get the classloader from an instantiated class, or pull the system class loader. (You probably wouldn't create one)

I.e. if you want to pull a file from the same package as your JsonObjectParser class:

<cfset obj = createobject("java", "your.JsonObjectParser").init(jsonFactory) />
<cfset stream = obj.getClass().getResourceAsStream("variables.json") />

would load your/variables.json from the same location as JsonObjectParser.

More likely you want something like
<cfset stream = createobject("java","java.io.FileInputStream").init(ExpandPath("./variables.json")) />

-G



--
Get 10% off of the regular price for this years CFCamp in Munich, Germany (Oct. 20th & 21st) with the Lucee discount code Lucee@cfcamp. 189€ instead of 210€. Visit https://ti.to/cfcamp/cfcamp-2016/discount/Lucee@cfcamp
---
You received this message because you are subscribed to the Google Groups "Lucee" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lucee+unsubscribe@googlegroups.com.
To post to this group, send email to lu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lucee/CAPURtC3UKDzik64uHy%2BRZYku2mjPW7Ajm%2B3GS82kUj_98mqAqA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

AJ Mercer

unread,
Nov 29, 2016, 11:15:31 PM11/29/16
to lu...@googlegroups.com
sorry, that is not very clear, variables.json contains the apsolute path to the file

FileInputStream does not have 'type' saved with it, so this fails
String fileType = (StringfileContents.get("type")

For more options, visit https://groups.google.com/d/optout.

Joseph Gooch

unread,
Nov 30, 2016, 6:59:28 AM11/30/16
to lu...@googlegroups.com
fileContents is populated by your parser... not by the stream... If I'm reading your code properly.

What should fileContents.get("type") return?

-G

Joseph Gooch

unread,
Nov 30, 2016, 7:04:09 AM11/30/16
to lu...@googlegroups.com
Based on your code, fileContents.get("type") isn't the mime-type of the file, it's the value of the "type" key within the structure represented in your json object inside your file.

i.e. the file probably has 
{
  "type": "something",
  ....
}

Markus Wollny

unread,
Dec 1, 2016, 10:17:18 AM12/1/16
to Lucee
If by chance what you are talking about is authentication for the Google API using the JSON credentials file (just a wild guess), here's what I do (note I use Mark Mandels tremendously useful class loader, so replace the local.objLoader with a createObject if you've got your classes in the global classpath):

variables.HTTP_Transport           = local.objLoader.create("com.google.api.client.http.javanet.NetHttpTransport").init();
variables.JSON_Factory             = local.objLoader.create("com.google.api.client.json.jackson2.JacksonFactory").init();
variables.WebmasterScopes = local.objLoader.create("com.google.api.services.webmasters.WebmastersScopes");
variables.GoogleCredentialBuilder = local.objLoader.create("com.google.api.client.googleapis.auth.oauth2.GoogleCredential$Builder")
.setTransport(variables.HTTP_Transport)
.setJsonFactory(variables.JSON_Factory)    
.build();
variables.keyFile = '/some/absolute/path/to/the/credentials.json';
...

local.keyFile = createObject("java", "java.io.File").init(variables.keyFile);
local.keyInputStream = createObject("java", "java.io.FileInputStream").init(local.keyFile);
local.credential = variables.GoogleCredentialBuilder    
    .fromStream(local.keyInputStream,variables.HTTP_Transport,variables.JSON_Factory)
    .createScoped(variables.Collections.singleton(variables.WebmasterScopes.WEBMASTERS_READONLY));
local.keyInputStream.close();

So you get the File-Object and open the InputStream on it, close it when you're done and you're all set. As far as I understand that is. Hope that helps :)

Kind regards

  Markus

AJ Mercer

unread,
Dec 1, 2016, 4:02:19 PM12/1/16
to lu...@googlegroups.com

That is what I am trying to do.

Thank you very much, this does look useful


--
Get 10% off of the regular price for this years CFCamp in Munich, Germany (Oct. 20th & 21st) with the Lucee discount code Lucee@cfcamp. 189€ instead of 210€. Visit https://ti.to/cfcamp/cfcamp-2016/discount/Lucee@cfcamp
---
You received this message because you are subscribed to the Google Groups "Lucee" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lucee+unsubscribe@googlegroups.com.
To post to this group, send email to lu...@googlegroups.com.

Markus Wollny

unread,
Dec 7, 2016, 9:48:37 AM12/7/16
to Lucee
Hi,

I have just published my code for accessing the Google Search Console API on Github, so if you or anyone else should need some hints for doing the JSON-file authentication dance with the Google API in CFML, you may find a working example there: https://github.com/Ratcreamsoup/CFML_googleSearchConsoleAPI

Kind regards

  Markus

AJ Mercer

unread,
Dec 7, 2016, 8:08:36 PM12/7/16
to lu...@googlegroups.com
Thanks for sharing.
I am am using ReST approach now - including oAuth2

--
You received this message because you are subscribed to the Google Groups "Lucee" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lucee+unsubscribe@googlegroups.com.
To post to this group, send email to lu...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages