Hi Team,
How to instatiate a servlet if runmode is dev or stage?
If it's path based we can get the runmode from backend and pass it to the fe and in fe we can check if the runmode from be is dev we can use ajax call and call servlet by path, but if it's resourcetype based servlet how to instatiate servlet by resourcetype based on dev runmode.
Views
Replies
Total Likes
Hi @Kk35 , If I understand correct, your requirement is to hit the servlet only is dev or stage env and you are worrying about automaticatic execution of servlet in case of resource based servlet. You can directly put a runmode check in resource based servlet so that it will prevent futher execution.
You can follow below approaches
1. There need not be an extra API call which tells you DEV or stage env. FE can always hit actual resource based servlet and in that servlet you can put a check if it is a DEV or STAGE runmode and break the flow. I would suggest this approach because run mode control check should always be at BE so that you will be in safe side even if someone calls the servlet that shouldn't run in DEV and it will stop the execution.
2. If you don't want FE calling a servlet always and you really need a servlet which tells runmode to FE then if you have actual resource based servlet then in this case also you need to put a run mode check.
Thanks
Ramesh
Views
Replies
Total Likes
Hi @Kk35
It doesn't matter whether your servlet is registered by resource type or path; you can use the approaches below to determine the run mode.
Further, based on servlet response, handle things in FE.
1. using SlingSettingService:
@Reference
private SlingSettingsService slingSettings;
private boolean enabled;
@Activate
@Modified
protected void activate() {
this.enabled = slingSettings.getRunModes().contains("dev") ||
slingSettings.getRunModes().contains("stage");
}
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
if (enabled) {
response.getWriter().write("Servlet is enabled!");
} else {
response.getWriter().write("Servlet is disabled!");
}
}
2. using custom configurations:
@Designate(ocd = MyServlet.Configuration.class)
public class MyServlet extends SlingAllMethodsServlet {
@ObjectClassDefinition(name = "My Servlet Configuration")
public @interface Configuration {
@AttributeDefinition(name = "Enabled", description = "Enable the servlet")
boolean enabled() default false;
}
private boolean enabled;
@Activate
@Modified
protected void activate(Configuration config) {
this.enabled = config.enabled();
}
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
if (enabled) {
response.getWriter().write("Servlet is enabled!");
} else {
response.getWriter().write("Servlet is disabled!");
}
}
3. using sling.runmodes property:
@Component(
service = Servlet.class,
property = {
"sling.servlet.paths=/bin/devstageServlet",
"sling.servlet.methods=GET",
"sling.runmodes=dev|stage" //Only register in dev or stage
}
)
public class DevStageServlet extends SlingSafeMethodsServlet {
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
response.getWriter().write("This servlet is only available in dev or stage environments.");
}
}
Views
Replies
Total Likes
hi @Kk35 - You can use https://medium.com/@vkarthik1356/alternative-osgi-code-for-slingsettingsservice-2ac6fcf71d53 and have the run mode based on environment picked.
Views
Replies
Total Likes