What is the process of reading a file in a service?

I am trying to initialize firebase, problem is I don’t know how to read the file properly.

I am getting this error: The issue is with the path.

 Could not find matching constructor for: java.io.FileInputStream(org.moqui.impl.context.reference.ComponentResourceReference)

Code:

        <actions>
            <script><![CDATA[
                import com.google.firebase.FirebaseApp
                import com.google.firebase.FirebaseOptions
                import com.google.auth.oauth2.GoogleCredentials

                def firebase = ec.resource.getLocationReference("component://sparts-workflow/data/Firebase.json")

                def serviceAccount = new FileInputStream(firebase)

                def options = new FirebaseOptions.Builder()
                        .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                        .build()

                FirebaseApp.initializeApp(options)
            ]]></script>
        </actions>

I also tried this way to read the file, but it doesn’t work

def firebase = ec.resource.getLocationReference("/sparts-workflow/data/Firebase.json")

But If I use an absolute path then I am able to read the file. This is the absolute path

def firebase = /home/fathe/Desktop/Pythys/sparts-moqui/runtime/component/sparts-workflow/data/Firebase.json

Try InputStream firebase = dataFile.openStream()

I use this in Java if that helps
keyPath = “component://sparts-workflow/data/Firebase.json”;
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(FileUtils.openInputStream(new File(ec.resource.getLocationReference(keyPath).getUri()))))
.build();

Thank you, Everyone.

1 Like