JAVA Question: getResourceAsStream()

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.InputStream getResourceAsStream(java.lang.String)

​But when I do this I get an error

myR = cl.getResourceAsStream(variables.json);

dump(myR);


[image: Inline images 1]

[image: 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 = (String) fileContents.get(“type”);
if (fileType == null) {
throw new IOException(“Error reading credentials from stream, ‘type’
field not specified.”);
}–

AJ Mercer
<webonix:net strength=“Industrial” /> http://webonix.net | <webonix:org
community=“Open” /> http://webonix.org
http://twitter.com/webonix

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"))
/>

-GOn Tue, Nov 29, 2016 at 2:50 AM, AJ Mercer <@AJ_Mercer> wrote:

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.InputStream getResourceAsStream(java.lang.String)

​But when I do this I get an error

myR = cl.getResourceAsStream(variables.json);

dump(myR);


[image: Inline images 1]

[image: 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 = (String) fileContents.get(“type”);
if (fileType == null) {
throw new IOException(“Error reading credentials from stream,
‘type’ field not specified.”);
}

AJ Mercer
<webonix:net strength=“Industrial” /> http://webonix.net | <webonix:org
community=“Open” /> http://webonix.org
http://twitter.com/webonix


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 lucee@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/
msgid/lucee/CAPURtC3UKDzik64uHy%2BRZYku2mjPW7Ajm%2B3GS82kUj_
98mqAqA%40mail.gmail.com
https://groups.google.com/d/msgid/lucee/CAPURtC3UKDzik64uHy%2BRZYku2mjPW7Ajm%2B3GS82kUj_98mqAqA%40mail.gmail.com?utm_medium=email&utm_source=footer
.
For more options, visit https://groups.google.com/d/optout.

fileContents is populated by your parser… not by the stream… If I’m
reading your code properly.

What should fileContents.get(“type”) return?

-GOn Tue, Nov 29, 2016 at 11:15 PM, AJ Mercer <@AJ_Mercer> wrote:

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 = (String) fileContents.get(“type”)

On 30 November 2016 at 09:29, Joseph Gooch <@Joseph_Gooch> wrote:

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.F
ileInputStream”).init(ExpandPath("./variables.json")) />

-G

On Tue, Nov 29, 2016 at 2:50 AM, AJ Mercer <@AJ_Mercer> wrote:

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.InputStream getResourceAsStream(java.lang.String)

​But when I do this I get an error

myR = cl.getResourceAsStream(variables.json);

dump(myR);


[image: Inline images 1]

[image: 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 = (String) fileContents.get(“type”);
if (fileType == null) {
throw new IOException(“Error reading credentials from stream,
‘type’ field not specified.”);
}

AJ Mercer
<webonix:net strength=“Industrial” /> http://webonix.net | <webonix:org
community=“Open” /> http://webonix.org
http://twitter.com/webonix


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-20
16/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 lucee@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/ms
gid/lucee/CAPURtC3UKDzik64uHy%2BRZYku2mjPW7Ajm%2B3GS82kUj_98
mqAqA%40mail.gmail.com
https://groups.google.com/d/msgid/lucee/CAPURtC3UKDzik64uHy%2BRZYku2mjPW7Ajm%2B3GS82kUj_98mqAqA%40mail.gmail.com?utm_medium=email&utm_source=footer
.
For more options, visit https://groups.google.com/d/optout.


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-20
16/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 lucee@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/ms
gid/lucee/CAKY58c5S11KKLG%3D%3DwEUabJM%2BZue%3DYeWazJ30CHnP
2S_V7oHjVA%40mail.gmail.com
https://groups.google.com/d/msgid/lucee/CAKY58c5S11KKLG%3D%3DwEUabJM%2BZue%3DYeWazJ30CHnP2S_V7oHjVA%40mail.gmail.com?utm_medium=email&utm_source=footer
.
For more options, visit https://groups.google.com/d/optout.

AJ Mercer
<webonix:net strength=“Industrial” /> http://webonix.net | <webonix:org
community=“Open” /> http://webonix.org
http://twitter.com/webonix


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 lucee@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/
msgid/lucee/CAPURtC31RdSPUkKcJZx_8Gr_gNPXaotvUwmX4sObk4jWTcT%3D4w%
40mail.gmail.com
https://groups.google.com/d/msgid/lucee/CAPURtC31RdSPUkKcJZx_8Gr_gNPXaotvUwmX4sObk4jWTcT%3D4w%40mail.gmail.com?utm_medium=email&utm_source=footer
.

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

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 = (String) fileContents.get(“type”)On 30 November 2016 at 09:29, Joseph Gooch <@Joseph_Gooch> wrote:

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

On Tue, Nov 29, 2016 at 2:50 AM, AJ Mercer <@AJ_Mercer> wrote:

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.InputStream getResourceAsStream(java.lang.String)

​But when I do this I get an error

myR = cl.getResourceAsStream(variables.json);

dump(myR);


[image: Inline images 1]

[image: 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 = (String) fileContents.get(“type”);
if (fileType == null) {
throw new IOException(“Error reading credentials from stream,
‘type’ field not specified.”);
}

AJ Mercer
<webonix:net strength=“Industrial” /> http://webonix.net | <webonix:org
community=“Open” /> http://webonix.org
http://twitter.com/webonix


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-20
16/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 lucee@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/ms
gid/lucee/CAPURtC3UKDzik64uHy%2BRZYku2mjPW7Ajm%2B3GS82kUj_98
mqAqA%40mail.gmail.com
https://groups.google.com/d/msgid/lucee/CAPURtC3UKDzik64uHy%2BRZYku2mjPW7Ajm%2B3GS82kUj_98mqAqA%40mail.gmail.com?utm_medium=email&utm_source=footer
.
For more options, visit https://groups.google.com/d/optout.


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 lucee@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/
msgid/lucee/CAKY58c5S11KKLG%3D%3DwEUabJM%2BZue%3DYeWazJ30CHnP2S_V7oHjVA%
40mail.gmail.com
https://groups.google.com/d/msgid/lucee/CAKY58c5S11KKLG%3D%3DwEUabJM%2BZue%3DYeWazJ30CHnP2S_V7oHjVA%40mail.gmail.com?utm_medium=email&utm_source=footer
.
For more options, visit https://groups.google.com/d/optout.

AJ Mercer
<webonix:net strength=“Industrial” /> http://webonix.net | <webonix:org
community=“Open” /> http://webonix.org
http://twitter.com/webonix

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”,

}On Wed, Nov 30, 2016 at 6:59 AM, Joseph Gooch <@Joseph_Gooch> wrote:

fileContents is populated by your parser… not by the stream… If I’m
reading your code properly.

What should fileContents.get(“type”) return?

-G

On Tue, Nov 29, 2016 at 11:15 PM, AJ Mercer <@AJ_Mercer> wrote:

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 = (String) fileContents.get(“type”)

On 30 November 2016 at 09:29, Joseph Gooch <@Joseph_Gooch> wrote:

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.F
ileInputStream”).init(ExpandPath("./variables.json")) />

-G

On Tue, Nov 29, 2016 at 2:50 AM, AJ Mercer <@AJ_Mercer> wrote:

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.InputStream getResourceAsStream(java.lang.String)

​But when I do this I get an error

myR = cl.getResourceAsStream(variables.json);

dump(myR);


[image: Inline images 1]

[image: 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 = (String) fileContents.get(“type”);
if (fileType == null) {
throw new IOException(“Error reading credentials from stream,
‘type’ field not specified.”);
}

AJ Mercer
<webonix:net strength=“Industrial” /> http://webonix.net | <webonix:org
community=“Open” /> http://webonix.org
http://twitter.com/webonix


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-20
16/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 lucee@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/ms
gid/lucee/CAPURtC3UKDzik64uHy%2BRZYku2mjPW7Ajm%2B3GS82kUj_98
mqAqA%40mail.gmail.com
https://groups.google.com/d/msgid/lucee/CAPURtC3UKDzik64uHy%2BRZYku2mjPW7Ajm%2B3GS82kUj_98mqAqA%40mail.gmail.com?utm_medium=email&utm_source=footer
.
For more options, visit https://groups.google.com/d/optout.


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-20
16/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 lucee@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/ms
gid/lucee/CAKY58c5S11KKLG%3D%3DwEUabJM%2BZue%3DYeWazJ30CHnP2
S_V7oHjVA%40mail.gmail.com
https://groups.google.com/d/msgid/lucee/CAKY58c5S11KKLG%3D%3DwEUabJM%2BZue%3DYeWazJ30CHnP2S_V7oHjVA%40mail.gmail.com?utm_medium=email&utm_source=footer
.
For more options, visit https://groups.google.com/d/optout.

AJ Mercer
<webonix:net strength=“Industrial” /> http://webonix.net | <webonix:org
community=“Open” /> http://webonix.org
http://twitter.com/webonix


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-20
16/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 lucee@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/ms
gid/lucee/CAPURtC31RdSPUkKcJZx_8Gr_gNPXaotvUwmX4sObk4jWTcT%
3D4w%40mail.gmail.com
https://groups.google.com/d/msgid/lucee/CAPURtC31RdSPUkKcJZx_8Gr_gNPXaotvUwmX4sObk4jWTcT%3D4w%40mail.gmail.com?utm_medium=email&utm_source=footer
.

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

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 :slight_smile:

Kind regards

Markus