Core implementation module containing the fundamental services, utilities, and infrastructure components for the Liferay Digital Experience Platform.
—
Core portal services providing business logic implementations, HTTP endpoints, and permission checking capabilities for the Liferay portal framework.
The service layer provides the main business logic implementations for portal entities and operations.
/**
* Base interface for all portal services providing common service patterns
*/
public interface BaseService {
/**
* Gets the service context for operations
* @return ServiceContext instance
*/
ServiceContext getServiceContext();
/**
* Sets the service context for operations
* @param serviceContext the service context to use
*/
void setServiceContext(ServiceContext serviceContext);
}
/**
* Local service interface providing direct access to service operations
*/
public interface LocalService extends BaseService {
/**
* Performs service operation with full privileges
* @param parameters operation parameters
* @return operation result
* @throws SystemException if operation fails
*/
Object performOperation(Object... parameters) throws SystemException;
/**
* Validates operation parameters and permissions
* @param parameters operation parameters
* @throws PortalException if validation fails
*/
void validateOperation(Object... parameters) throws PortalException;
}
/**
* Remote service interface providing secure remote access to service operations
*/
public interface RemoteService extends BaseService {
/**
* Performs service operation with permission checking
* @param parameters operation parameters
* @return operation result
* @throws SystemException if operation fails
* @throws PrincipalException if permission denied
*/
Object performSecureOperation(Object... parameters)
throws SystemException, PrincipalException;
}HTTP-specific service implementations providing REST and SOAP endpoints for portal services.
/**
* Base HTTP service providing common HTTP endpoint functionality
*/
public abstract class BaseHTTPService {
/**
* Processes HTTP GET requests
* @param request HTTP servlet request
* @param response HTTP servlet response
* @throws ServletException if request processing fails
* @throws IOException if I/O error occurs
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException;
/**
* Processes HTTP POST requests
* @param request HTTP servlet request
* @param response HTTP servlet response
* @throws ServletException if request processing fails
* @throws IOException if I/O error occurs
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException;
/**
* Processes HTTP PUT requests
* @param request HTTP servlet request
* @param response HTTP servlet response
* @throws ServletException if request processing fails
* @throws IOException if I/O error occurs
*/
protected void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException;
/**
* Processes HTTP DELETE requests
* @param request HTTP servlet request
* @param response HTTP servlet response
* @throws ServletException if request processing fails
* @throws IOException if I/O error occurs
*/
protected void doDelete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException;
}
/**
* JSON web service providing REST API endpoints
*/
public class JSONWebService extends BaseHTTPService {
/**
* Processes JSON-based web service requests
* @param serviceName the service to invoke
* @param methodName the method to call
* @param parameters method parameters as JSON
* @return JSON response
* @throws Exception if service invocation fails
*/
public String invokeService(String serviceName, String methodName, String parameters)
throws Exception;
/**
* Serializes object to JSON format
* @param object the object to serialize
* @return JSON string representation
*/
protected String toJSON(Object object);
/**
* Deserializes JSON to object
* @param json JSON string
* @param clazz target class type
* @return deserialized object
*/
protected <T> T fromJSON(String json, Class<T> clazz);
}
/**
* SOAP web service providing SOAP API endpoints
*/
public class SOAPWebService extends BaseHTTPService {
/**
* Processes SOAP-based web service requests
* @param soapAction the SOAP action to perform
* @param soapMessage the SOAP message body
* @return SOAP response message
* @throws Exception if SOAP processing fails
*/
public String processSoapRequest(String soapAction, String soapMessage)
throws Exception;
/**
* Generates WSDL definition for the service
* @return WSDL XML definition
*/
public String generateWSDL();
}Common utilities and helpers used across the service layer.
/**
* Service context providing request context and user information
*/
public class ServiceContext {
/**
* Gets the current user ID
* @return user ID
*/
public long getUserId();
/**
* Sets the current user ID
* @param userId the user ID
*/
public void setUserId(long userId);
/**
* Gets the current company ID
* @return company ID
*/
public long getCompanyId();
/**
* Sets the current company ID
* @param companyId the company ID
*/
public void setCompanyId(long companyId);
/**
* Gets request attributes
* @return Map of request attributes
*/
public Map<String, Serializable> getAttributes();
/**
* Sets request attributes
* @param attributes Map of attributes to set
*/
public void setAttributes(Map<String, Serializable> attributes);
/**
* Gets workflow action if applicable
* @return workflow action constant
*/
public int getWorkflowAction();
/**
* Sets workflow action
* @param workflowAction the workflow action constant
*/
public void setWorkflowAction(int workflowAction);
}
/**
* Service locator for finding and accessing portal services
*/
public class ServiceLocator {
/**
* Locates local service by name
* @param serviceName the service name
* @return local service instance
* @throws ServiceException if service not found
*/
public static Object locateLocalService(String serviceName) throws ServiceException;
/**
* Locates remote service by name
* @param serviceName the service name
* @return remote service instance
* @throws ServiceException if service not found
*/
public static Object locateRemoteService(String serviceName) throws ServiceException;
/**
* Registers a service implementation
* @param serviceName the service name
* @param serviceImpl the service implementation
*/
public static void registerService(String serviceName, Object serviceImpl);
}Security layer providing permission validation for service operations.
/**
* Base permission checker for portal resources
*/
public abstract class BasePermissionChecker {
/**
* Checks if user has permission for the specified action on resource
* @param userId the user ID
* @param resourceName the resource name
* @param resourcePrimKey the resource primary key
* @param actionId the action ID
* @return true if user has permission
* @throws SystemException if permission check fails
*/
public abstract boolean hasPermission(long userId, String resourceName,
String resourcePrimKey, String actionId) throws SystemException;
/**
* Checks if user is owner of the resource
* @param userId the user ID
* @param resourceOwnerId the resource owner ID
* @return true if user is owner
*/
protected boolean isOwner(long userId, long resourceOwnerId);
/**
* Checks if user has administrative privileges
* @param userId the user ID
* @param companyId the company ID
* @return true if user is administrator
* @throws SystemException if check fails
*/
protected boolean isCompanyAdmin(long userId, long companyId) throws SystemException;
}
/**
* Resource permission checker for specific resource types
*/
public class ResourcePermissionChecker extends BasePermissionChecker {
/**
* Checks resource permissions with role-based access control
* @param userId the user ID
* @param resourceName the resource name
* @param resourcePrimKey the resource primary key
* @param actionId the action ID
* @return true if user has permission
* @throws SystemException if permission check fails
*/
@Override
public boolean hasPermission(long userId, String resourceName,
String resourcePrimKey, String actionId) throws SystemException;
/**
* Checks if user belongs to required role
* @param userId the user ID
* @param roleId the role ID
* @return true if user has role
*/
public boolean hasRole(long userId, long roleId);
/**
* Gets user's permissions for a resource
* @param userId the user ID
* @param resourceName the resource name
* @param resourcePrimKey the resource primary key
* @return List of action IDs user can perform
*/
public List<String> getResourceActions(long userId, String resourceName,
String resourcePrimKey);
}
/**
* Model permission checker for entity-specific permissions
*/
public class ModelPermissionChecker extends BasePermissionChecker {
/**
* Checks model-level permissions
* @param userId the user ID
* @param model the model instance
* @param actionId the action ID
* @return true if user has permission on model
* @throws SystemException if permission check fails
*/
public boolean hasModelPermission(long userId, Object model, String actionId)
throws SystemException;
/**
* Filters list of models based on user permissions
* @param userId the user ID
* @param models list of models to filter
* @param actionId the required action
* @return filtered list of models user can access
*/
public <T> List<T> filterByPermission(long userId, List<T> models, String actionId);
}Constants used by the persistence layer for finder operations and queries.
/**
* Constants for user group finder operations
*/
public class UserGroupFinderConstants {
/**
* Finder method name for finding user groups by organization
*/
public static final String FIND_BY_ORGANIZATION = "findByOrganization";
/**
* Finder method name for finding user groups by site
*/
public static final String FIND_BY_SITE = "findBySite";
/**
* Finder method name for finding user groups by user
*/
public static final String FIND_BY_USER = "findByUser";
/**
* Parameter name for organization ID
*/
public static final String PARAM_ORGANIZATION_ID = "organizationId";
/**
* Parameter name for site ID
*/
public static final String PARAM_SITE_ID = "siteId";
/**
* Parameter name for user ID
*/
public static final String PARAM_USER_ID = "userId";
}Service Implementation:
// Custom local service implementation
public class CustomLocalServiceImpl implements CustomLocalService {
@Override
public CustomEntity addCustomEntity(String name, String description,
ServiceContext serviceContext) throws SystemException, PortalException {
// Validate parameters
validateAddCustomEntity(name, description);
// Create entity
CustomEntity entity = new CustomEntity();
entity.setName(name);
entity.setDescription(description);
entity.setUserId(serviceContext.getUserId());
entity.setCompanyId(serviceContext.getCompanyId());
return customEntityPersistence.update(entity);
}
private void validateAddCustomEntity(String name, String description)
throws PortalException {
if (name == null || name.trim().isEmpty()) {
throw new CustomEntityNameException("Name is required");
}
if (description == null || description.trim().isEmpty()) {
throw new CustomEntityDescriptionException("Description is required");
}
}
}HTTP Service Endpoint:
// JSON web service endpoint
public class CustomJSONWebService extends JSONWebService {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
try {
if ("getCustomEntities".equals(action)) {
List<CustomEntity> entities = customLocalService.getCustomEntities();
String json = toJSON(entities);
writeResponse(response, json);
}
} catch (Exception e) {
writeErrorResponse(response, e.getMessage());
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String json = readRequestBody(request);
CustomEntity entity = fromJSON(json, CustomEntity.class);
ServiceContext serviceContext = createServiceContext(request);
CustomEntity result = customLocalService.addCustomEntity(
entity.getName(), entity.getDescription(), serviceContext);
writeResponse(response, toJSON(result));
} catch (Exception e) {
writeErrorResponse(response, e.getMessage());
}
}
}Permission Checking:
// Custom permission checker
public class CustomPermissionChecker extends BasePermissionChecker {
@Override
public boolean hasPermission(long userId, String resourceName,
String resourcePrimKey, String actionId) throws SystemException {
// Check basic permissions
if (isCompanyAdmin(userId, getCompanyId())) {
return true;
}
// Check resource-specific permissions
if ("CUSTOM_ENTITY".equals(resourceName)) {
return hasCustomEntityPermission(userId, resourcePrimKey, actionId);
}
return false;
}
private boolean hasCustomEntityPermission(long userId, String entityId,
String actionId) throws SystemException {
CustomEntity entity = customEntityPersistence.findByPrimaryKey(
Long.parseLong(entityId));
// Owner can perform all actions
if (isOwner(userId, entity.getUserId())) {
return true;
}
// Check role-based permissions
if ("VIEW".equals(actionId)) {
return hasRole(userId, RoleConstants.SITE_MEMBER);
} else if ("UPDATE".equals(actionId) || "DELETE".equals(actionId)) {
return hasRole(userId, RoleConstants.SITE_ADMINISTRATOR);
}
return false;
}
}The service layer provides the foundation for all portal functionality:
Services are typically configured through Spring configuration:
<!-- Local service configuration -->
<bean id="customLocalService"
class="com.company.service.impl.CustomLocalServiceImpl">
<property name="customEntityPersistence" ref="customEntityPersistence"/>
</bean>
<!-- Remote service configuration -->
<bean id="customRemoteService"
class="com.company.service.impl.CustomRemoteServiceImpl">
<property name="customLocalService" ref="customLocalService"/>
</bean>
<!-- HTTP service configuration -->
<bean id="customJSONWebService"
class="com.company.service.http.CustomJSONWebService">
<property name="customLocalService" ref="customLocalService"/>
</bean>Service layer error handling follows portal conventions:
Best practices include comprehensive input validation, proper exception handling, detailed error logging, and returning meaningful error messages to clients while maintaining security.
Install with Tessl CLI
npx tessl i tessl/maven-com-liferay-portal--com-liferay-portal-impl