Skip to content

Commit

Permalink
Add servlet API
Browse files Browse the repository at this point in the history
  • Loading branch information
shanggeeth committed Dec 10, 2024
1 parent 4bc3bb1 commit ddb92b2
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
com.nimbusds.jwt; version="${nimbusds.osgi.version.range}",
javax.servlet.http; version="${imp.pkg.version.javax.servlet}"
</Import-Package>
<DynamicImport-Package>*</DynamicImport-Package>
<Export-Package>
!org.wso2.carbon.identity.user.self.registration.internal,
org.wso2.carbon.identity.user.self.registration.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.equinox.http.helper.ContextPathServletAdaptor;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
Expand All @@ -27,13 +28,17 @@
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.http.HttpService;
import org.wso2.carbon.identity.application.mgt.ApplicationManagementService;
import org.wso2.carbon.identity.user.self.registration.UserRegistrationFlowService;
import org.wso2.carbon.identity.user.self.registration.executor.Executor;
import org.wso2.carbon.identity.user.self.registration.executor.impl.AttributeCollectorImpl;
import org.wso2.carbon.identity.user.self.registration.servlet.RegistrationOrchastrationServlet;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.user.core.service.RealmService;

import javax.servlet.Servlet;

@Component(
name = "user.self.registration.component",
immediate = true)
Expand All @@ -43,6 +48,7 @@ public class UserRegistrationDSComponent {

private static BundleContext bundleContext = null;
private static RegistryService registryService = null;
private HttpService httpService;
private static RealmService realmService = null;
public static RegistryService getRegistryService() {
return registryService;
Expand All @@ -55,6 +61,13 @@ protected void activate(ComponentContext context) {
bundleContext = context.getBundleContext();
bundleContext.registerService(UserRegistrationFlowService.class.getName(), UserRegistrationFlowService.getInstance(), null);
bundleContext.registerService(Executor.class.getName(), attributeCollectionExecutor, null);

Servlet registrationOrchastrationServlet = new ContextPathServletAdaptor(new RegistrationOrchastrationServlet(), "/registration-orchestration");
try {
httpService.registerServlet("/registration-orchestration", registrationOrchastrationServlet, null, null);
} catch (Throwable e) {
log.error("Error when registering RegistrationOrchastrationServlet via the OSGi HttpService.", e);
}
}

@Deactivate
Expand Down Expand Up @@ -139,5 +152,28 @@ protected void unsetExecutors(Executor executor) {

UserRegistrationServiceDataHolder.getExecutors().remove(executor);
}

@Reference(
name = "osgi.http.service",
service = HttpService.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetHttpService"
)
protected void setHttpService(HttpService httpService) {

if (log.isDebugEnabled()) {
log.debug("HTTP Service is set in Trusted App mgt bundle");
}
this.httpService = httpService;
}

protected void unsetHttpService(HttpService httpService) {

if (log.isDebugEnabled()) {
log.debug("HTTP Service is unset in the Trusted App mgt bundle");
}
this.httpService = null;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com) All Rights Reserved.
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.identity.user.self.registration.servlet;

import java.io.IOException;
import java.util.stream.Collectors;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RegistrationOrchastrationServlet extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String username = request.getParameter("app");

// Logic to get the app name from path
// the url should look like this: http://localhost:9443/registration-orchestration/app/2347987234

String[] pathParts = request.getRequestURI().split("/");
String appName = pathParts[pathParts.length - 1];

// get the json body which contains the registration flow data
String jsonBody = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));

// logic to register the registration flow data to the app

// Return a dummy json response with the app name and the registration flow data
response.setContentType("application/json");
response.getWriter().write("{\"app\":\"" + appName + "\", \"registrationFlowData\":" + jsonBody + "}");
response.setStatus(HttpServletResponse.SC_OK);

}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// get the app name from the path and return the app name as a json response

String[] pathParts = request.getRequestURI().split("/");
String appName = pathParts[pathParts.length - 1];

}
}
1 change: 1 addition & 0 deletions components/user-mgt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<module>org.wso2.carbon.identity.user.profile</module>
<module>org.wso2.carbon.identity.user.profile.ui</module>
<module>org.wso2.carbon.identity.user.registration</module>
<module>org.wso2.carbon.identity.user.self.registration</module>
</modules>

</project>

0 comments on commit ddb92b2

Please sign in to comment.