getting FirebaseApp with name [DEFAULT] doesn't exist after deploying spring boot jar file on AWS beanstalk - spring

I have a spring boot rest api which uploads some documents on firestore DB. Problem is when I am running locally it is working absolutely fine without causing any issue. but when I am packaging it as a jar and uploading in AWS beanstalk. That endpoint giving below error response.
{
"timestamp": "2022-11-01T13:53:21.121+00:00",
"status": 500,
"error": "Internal Server Error",
"message": "FirebaseApp with name [DEFAULT] doesn't exist. ",
}
This is how I am reading the firebase service account (file name is serviceaccount.json) file which is located in src/main/resources folder and I have
firebase.credential.resource=serviceaccount.json entry in my application.properties
#Value("${firebase.credential.resource}")
String resourcePath;
#PostConstruct
public void initialize() {
try {
Resource resource = new ClassPathResource(resourcePath);
//FileInputStream serviceAccount = new FileInputStream(resource.getFile());
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(resource.getInputStream()))
.build();
if (FirebaseApp.getApps().isEmpty()) {
FirebaseApp.initializeApp(options);
}
} catch (Exception e) {
e.printStackTrace();
}
}

Related

Upload file to ec2 docker instance from spring boot

I have written a spring boot application rest-api to upload file on docker instance inside ec2.
Though I am not getting any errors, the file is not being uploaded to docker container.
#PostMapping(value = "/uploadFile")
public void uploadFile(#RequestPart(name = "file") MultipartFile file)
{
try {
System.out.println("Writing file");
byte[] bytes = file.getBytes();
Path path =Paths.get(uploadPath +file.getOriginalFilename());
System.out.println("Uploaded to :"+uploadPath+file.getOriginalFilename());
Files.write(path, file.getBytes());
}
catch (Exception e) {
System.out.println(e);
}
}
filepath = /home/ubuntu/dockerproject/file-upload/
What I want to achieve is that using the above rest api , the file should get uploaded to file-path location.
I am not able to rectify if the path is wrong or some other issue exists.
Also file-upload has permission 0777.

Push Notification with FCM + Spring Boot, SenderId mismatch error

I'm doing a project using Spring Boot to send notifications to my React Native app. Since I'm using Expo to my React Native app, I did a different configuration following this tutorial and tested with Notifications composer from Firebase and worked well.
For the Spring Boot, I used this another tutorial to configure FCM, also I create a Private Key file from SDK admin in Firebase to use here:
try {
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(new ClassPathResource(MY_PRIVATE_KEY_FILE).getInputStream()))
.build();
if (FirebaseApp.getApps().isEmpty()) {
FirebaseApp.initializeApp(options);
}
} catch (IOException e) {
e.printStackTrace();
}
So I did a method to send the message using token from Server Key like this:
private String token="AAAAm_p0hoc:************************************";
public void sendPushNotificationWithData() {
PushNotificationRequest pushNotificationRequest = new PushNotificationRequest();
pushNotificationRequest.setMessage("Send push notifications from Spring Boot server");
pushNotificationRequest.setTitle("test Push Notification");
pushNotificationRequest.setToken(token);
Map<String, String> appData= new HashMap<>();
appData.put("name", "PushNotification");
try {
fcmService.sendMessage(appData, pushNotificationRequest);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
But I'm just having this response from FCM:
POST https://fcm.googleapis.com/v1/projects/myproject/messages:send
{
"error": {
"code": 403,
"message": "SenderId mismatch",
"status": "PERMISSION_DENIED",
"details": [
{
"#type": "type.googleapis.com/google.firebase.fcm.v1.FcmError",
"errorCode": "SENDER_ID_MISMATCH"
}
]
}
}
I tried to make a new project into FireBase, update the private and server keys but still having the same response.
Is something I forgot to set up or am I doing it wrong?

Spring Boot multiple WAR files in 1 Tomcat

we are developing a multi-mandator shop solution for multiple countries, like Sweden, Netherlands, Germany etc. We aim to have 1 WAR file for each mandator and would like to have all of them running in 1 tomcat. Is it possible to have this integrated into Spring-Boot's embedded tomcat?
If the Mandators are different webapps/war files then you can add wars/web apps to the EmbeddedServletContainer(Tomcat), using the tomcat.addWebapp method.
In your spring-boot main class add the following bean.
#Bean
public EmbeddedServletContainerFactory servletContainerFactory() {
return new TomcatEmbeddedServletContainerFactory() {
#Override
protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
Tomcat tomcat) {
// Ensure that the webapps directory exists
new File(tomcat.getServer().getCatalinaBase(), "webapps").mkdirs();
try {
Context context = tomcat.addWebapp("/Sweden","Sweden.war");
tomcat.addWebapp("/Netherlands","Netherlands.war");
tomcat.addWebapp("/Germany","Germany.war");
context.setParentClassLoader(getClass().getClassLoader());
} catch (ServletException ex) {
throw new IllegalStateException("Failed to add webapp", ex);
}
return super.getTomcatEmbeddedServletContainer(tomcat);
}
};
}

Error in initialising portlets on portal server

I have a WebSphere Portal server that houses portlets. The portlets make remote calls to ejbs running on WAS. Both the portal server and the WAS are running on the local machine. The WAS starts up properly, but on trying to start up the portal server, it throws the following error:
nested exception is: java.lang.NullPointerException at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
On looking up the source in spring, I find the method definition:
public PropertyValues postProcessPropertyValues(
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
InjectionMetadata metadata = findAutowiringMetadata(bean.getClass());
try {
metadata.injectMethods(bean, beanName, pvs);
}
catch (Throwable ex) {
throw new BeanCreationException(beanName, "Autowiring of methods failed", ex);
}
return pvs;
}
Since the error is in spring source code and not my code, how should I start debugging it?

Spring RMI non-JRMP server at remote endpoint

I'm running the following code:
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class RmiClient {
public static void main(String args[]) {
try {
String hostName = "hostnameChangedForOnlineReference";
Registry registry = LocateRegistry.getRegistry(hostName, 1099);
String[] names = registry.list();
for (String name : names) {
System.out.println(name);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
And getting the following error
java.rmi.ConnectIOException: non-JRMP server at remote endpoint
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:230)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:322)
at sun.rmi.registry.RegistryImpl_Stub.list(Unknown Source)
at RmiClient.main(RmiClient.java:9)
Yet other RMI code running on my machine connects to this same service just fine.
Can anyone suggest a path of investigation for this inconsistent behaviour on my machine?
(When I do rmi in spring I get exactly the same error, but this is an easier set of code to digest).
non-JRMP server at remote endpoint
means that it turns out turning on SSL was required.

Resources