Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package org.wso2.carbon.identity.api.server.extension.management.common;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.extension.mgt.ExtensionManager;

Expand All @@ -26,6 +28,8 @@
*/
public class ExtensionManagementServiceHolder {

private static final Log log = LogFactory.getLog(ExtensionManagementServiceHolder.class);

private ExtensionManagementServiceHolder() {}

private static class ExtensionManagerHolder {
Expand All @@ -41,6 +45,13 @@ private static class ExtensionManagerHolder {
*/
public static ExtensionManager getExtensionManager() {

return ExtensionManagerHolder.SERVICE;
if (log.isDebugEnabled()) {
log.debug("Retrieving ExtensionManager OSGi service.");
}
ExtensionManager extensionManager = ExtensionManagerHolder.SERVICE;
if (extensionManager == null) {
log.warn("ExtensionManager OSGi service is not available.");
}
return extensionManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package org.wso2.carbon.identity.api.server.extension.management.common.utils;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.api.server.common.Constants;
import org.wso2.carbon.identity.api.server.common.ContextLoader;
import org.wso2.carbon.identity.api.server.common.error.APIError;
Expand All @@ -27,13 +29,16 @@

import javax.ws.rs.core.Response;

import static org.wso2.carbon.identity.api.server.extension.management.common.utils.ExtensionMgtConstants.EXTENSION_MGT_PATH_COMPONENT;
import static org.wso2.carbon.identity.api.server.extension.management.common.utils.ExtensionMgtConstants
.EXTENSION_MGT_PATH_COMPONENT;

/**
* Utility class for extension management.
*/
public class ExtensionMgtUtils {

private static final Log log = LogFactory.getLog(ExtensionMgtUtils.class);

/**
* Get the path of the extension type.
*
Expand All @@ -42,6 +47,11 @@ public class ExtensionMgtUtils {
*/
public static String getExtensionInfoLocation(String extensionType, String extensionId) {

if (log.isDebugEnabled()) {
log.debug("Building extension info location for type: " +
(extensionType != null ? extensionType : "null") + ", id: " +
(extensionId != null ? extensionId : "null"));
}
return ContextLoader.buildURIForBody(
Constants.V1_API_PATH_COMPONENT + EXTENSION_MGT_PATH_COMPONENT + '/' +
extensionType + "/" + extensionId).toString();
Expand All @@ -58,6 +68,13 @@ public static String getExtensionInfoLocation(String extensionType, String exten
public static APIError handleClientException(Response.Status status, ExtensionMgtConstants.ErrorMessage error,
String... data) {

if (error == null) {
throw new IllegalArgumentException("Error message cannot be null.");
}
if (log.isDebugEnabled()) {
log.debug("Handling client exception with status: " + status + ", error code: " +
error.getCode());
}
return new APIError(status, getErrorBuilder(error, data).build());
}

Expand All @@ -72,6 +89,11 @@ public static APIError handleClientException(Response.Status status, ExtensionMg
public static APIError handleServerException(Response.Status status, ExtensionMgtConstants.ErrorMessage error,
String... data) {

if (error == null) {
throw new IllegalArgumentException("Error message cannot be null.");
}
log.error("Server exception occurred with status: " + status + ", error code: " +
error.getCode());
return new APIError(status, getErrorBuilder(error, data).build());
}

Expand Down Expand Up @@ -100,8 +122,12 @@ private static ErrorResponse.Builder getErrorBuilder(ExtensionMgtConstants.Error
*/
public static void validateExtensionType(String extensionType) {

if (log.isDebugEnabled()) {
log.debug("Validating extension type: " + (extensionType != null ? extensionType : "null"));
}
if (!ArrayUtils.contains(ExtensionManagementServiceHolder.getExtensionManager()
.getExtensionTypes(), extensionType)) {
log.warn("Invalid extension type provided: " + (extensionType != null ? extensionType : "null"));
throw handleClientException(Response.Status.BAD_REQUEST, ExtensionMgtConstants.ErrorMessage
.ERROR_CODE_INVALID_EXTENSION_TYPE, extensionType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ public class ExtensionListItemBuilder implements Function<ExtensionInfo, Extensi
@Override
public ExtensionListItem apply(ExtensionInfo extensionInfo) {

if (log.isDebugEnabled()) {
log.debug("Building ExtensionListItem for extension: " +
(extensionInfo != null ? extensionInfo.getId() : "null"));
}

if (extensionInfo == null) {
log.warn("ExtensionInfo is null. Cannot build ExtensionListItem.");
return null;
}

ExtensionListItem extensionListItem = new ExtensionListItem();
extensionListItem.setId(extensionInfo.getId());
extensionListItem.setVersion(extensionInfo.getVersion());
Expand All @@ -51,6 +61,11 @@ public ExtensionListItem apply(ExtensionInfo extensionInfo) {
if (extensionInfo.getCustomAttributes() != null) {
extensionListItem.setCustomAttributes(extensionInfo.getCustomAttributes());
}

if (log.isDebugEnabled()) {
log.debug("Successfully built ExtensionListItem for extension: " + extensionInfo.getId());
}

return extensionListItem;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package org.wso2.carbon.identity.api.server.extension.management.v1.function;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.api.server.extension.management.v1.model.ExtensionResponseModel;
import org.wso2.carbon.identity.extension.mgt.model.ExtensionInfo;

Expand All @@ -28,9 +30,21 @@
*/
public class ExtensionResponseModelBuilder implements Function<ExtensionInfo, ExtensionResponseModel> {

private static final Log log = LogFactory.getLog(ExtensionResponseModelBuilder.class);

@Override
public ExtensionResponseModel apply(ExtensionInfo extensionInfo) {

if (log.isDebugEnabled()) {
log.debug("Building ExtensionResponseModel for extension: " +
(extensionInfo != null ? extensionInfo.getId() : "null"));
}

if (extensionInfo == null) {
log.warn("ExtensionInfo is null. Cannot build ExtensionResponseModel.");
return null;
}

ExtensionResponseModel responseModel = new ExtensionResponseModel();
responseModel.setId(extensionInfo.getId());
responseModel.setVersion(extensionInfo.getVersion());
Expand All @@ -41,6 +55,11 @@ public ExtensionResponseModel apply(ExtensionInfo extensionInfo) {
responseModel.setTags(extensionInfo.getTags());
responseModel.setCategory(extensionInfo.getCategory());
responseModel.setType(extensionInfo.getType());

if (log.isDebugEnabled()) {
log.debug("Successfully built ExtensionResponseModel for extension: " + extensionInfo.getId());
}

return responseModel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package org.wso2.carbon.identity.api.server.extension.management.v1.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;
import org.wso2.carbon.identity.api.server.extension.management.common.ExtensionManagementServiceHolder;
import org.wso2.carbon.identity.api.server.extension.management.common.utils.ExtensionMgtConstants;
Expand All @@ -40,6 +42,8 @@
*/
public class ExtensionsApiServiceImpl implements ExtensionsApiService {

private static final Log log = LogFactory.getLog(ExtensionsApiServiceImpl.class);

/**
* Get all the extensions.
*
Expand All @@ -48,8 +52,17 @@ public class ExtensionsApiServiceImpl implements ExtensionsApiService {
@Override
public Response listExtensions() {

if (log.isDebugEnabled()) {
log.debug("Retrieving all extensions");
}

List<ExtensionInfo> extensionInfoList = ExtensionManagementServiceHolder.getExtensionManager()
.getExtensions();

if (log.isDebugEnabled()) {
log.debug("Found " + extensionInfoList.size() + " extensions");
}

return Response.ok().entity(extensionInfoList.stream().map(new
ExtensionListItemBuilder()).collect(Collectors.toList())).build();
}
Expand All @@ -63,14 +76,24 @@ public Response listExtensions() {
@Override
public Response listExtensionsByType(String extensionType) {

if (log.isDebugEnabled()) {
log.debug("Retrieving extensions by type: " + extensionType);
}

// TODO: Add pagination support.
validateExtensionType(extensionType);
try {
List<ExtensionInfo> extensionInfoList = ExtensionManagementServiceHolder.getExtensionManager()
.getExtensionsByType(extensionType);

if (log.isDebugEnabled()) {
log.debug("Found " + extensionInfoList.size() + " extensions for type: " + extensionType);
}

return Response.ok().entity(extensionInfoList.stream().map(new
ExtensionListItemBuilder()).collect(Collectors.toList())).build();
} catch (ExtensionManagementException e) {
log.warn("Failed to retrieve extensions by type: " + extensionType + ". Error: " + e.getMessage());
throw ExtensionMgtUtils.handleServerException(Response.Status.INTERNAL_SERVER_ERROR,
ExtensionMgtConstants.ErrorMessage.ERROR_CODE_ERROR_GETTING_EXTENSIONS_BY_TYPE, extensionType);
}
Expand All @@ -86,16 +109,29 @@ public Response listExtensionsByType(String extensionType) {
@Override
public Response getExtensionInfoById(String extensionType, String extensionId) {

if (log.isDebugEnabled()) {
log.debug("Retrieving extension info for type: " + extensionType + ", id: " + extensionId);
}

validateExtensionType(extensionType);
try {
ExtensionInfo extensionInfo = ExtensionManagementServiceHolder.getExtensionManager()
.getExtensionByTypeAndId(extensionType, extensionId);
if (extensionInfo == null) {
log.warn("Extension not found for type: " + extensionType + ", id: " + extensionId);
throw ExtensionMgtUtils.handleClientException(Response.Status.NOT_FOUND,
ExtensionMgtConstants.ErrorMessage.ERROR_CODE_EXTENSION_NOT_FOUND, extensionId, extensionType);
}

if (log.isDebugEnabled()) {
log.debug("Successfully retrieved extension info for type: " + extensionType + ", id: " +
extensionId);
}

return Response.ok().entity(new ExtensionResponseModelBuilder().apply(extensionInfo)).build();
} catch (ExtensionManagementException e) {
log.warn("Failed to retrieve extension for type: " + extensionType + ", id: " + extensionId +
". Error: " + e.getMessage());
throw ExtensionMgtUtils.handleServerException(Response.Status.INTERNAL_SERVER_ERROR,
ExtensionMgtConstants.ErrorMessage.ERROR_CODE_ERROR_GETTING_EXTENSION, extensionId, extensionType);
}
Expand All @@ -111,16 +147,29 @@ public Response getExtensionInfoById(String extensionType, String extensionId) {
@Override
public Response getTemplateById(String extensionType, String extensionId) {

if (log.isDebugEnabled()) {
log.debug("Retrieving template for extension type: " + extensionType + ", id: " + extensionId);
}

validateExtensionType(extensionType);
try {
JSONObject template = ExtensionManagementServiceHolder.getExtensionManager()
.getExtensionTemplate(extensionType, extensionId);
if (template == null) {
log.warn("Template not found for extension type: " + extensionType + ", id: " + extensionId);
throw ExtensionMgtUtils.handleClientException(Response.Status.NOT_FOUND,
ExtensionMgtConstants.ErrorMessage.ERROR_CODE_TEMPLATE_NOT_FOUND, extensionId, extensionType);
}

if (log.isDebugEnabled()) {
log.debug("Successfully retrieved template for extension type: " + extensionType + ", id: " +
extensionId);
}

return Response.ok().entity(template.toString()).build();
} catch (ExtensionManagementException e) {
log.warn("Failed to retrieve template for extension type: " + extensionType + ", id: " + extensionId +
". Error: " + e.getMessage());
throw ExtensionMgtUtils.handleServerException(Response.Status.INTERNAL_SERVER_ERROR,
ExtensionMgtConstants.ErrorMessage.ERROR_CODE_ERROR_GETTING_TEMPLATE, extensionId, extensionType);
}
Expand All @@ -136,16 +185,29 @@ public Response getTemplateById(String extensionType, String extensionId) {
@Override
public Response getMetadataById(String extensionType, String extensionId) {

if (log.isDebugEnabled()) {
log.debug("Retrieving metadata for extension type: " + extensionType + ", id: " + extensionId);
}

validateExtensionType(extensionType);
try {
JSONObject metadata = ExtensionManagementServiceHolder.getExtensionManager()
.getExtensionMetadata(extensionType, extensionId);
if (metadata == null) {
log.warn("Metadata not found for extension type: " + extensionType + ", id: " + extensionId);
throw ExtensionMgtUtils.handleClientException(Response.Status.NOT_FOUND,
ExtensionMgtConstants.ErrorMessage.ERROR_CODE_METADATA_NOT_FOUND, extensionId, extensionType);
}

if (log.isDebugEnabled()) {
log.debug("Successfully retrieved metadata for extension type: " + extensionType + ", id: " +
extensionId);
}

return Response.ok().entity(metadata.toString()).build();
} catch (ExtensionManagementException e) {
log.warn("Failed to retrieve metadata for extension type: " + extensionType + ", id: " + extensionId +
". Error: " + e.getMessage());
throw ExtensionMgtUtils.handleServerException(Response.Status.INTERNAL_SERVER_ERROR,
ExtensionMgtConstants.ErrorMessage.ERROR_CODE_ERROR_GETTING_METADATA, extensionId, extensionType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package org.wso2.carbon.identity.api.server.fetch.remote.common;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.remotefetch.common.RemoteFetchConfigurationService;

Expand All @@ -26,6 +28,8 @@
*/
public class RemoteFetchServiceHolder {

private static final Log log = LogFactory.getLog(RemoteFetchServiceHolder.class);

private RemoteFetchServiceHolder() {}

private static class RemoteFetchConfigurationServiceHolder {
Expand All @@ -41,6 +45,15 @@ private static class RemoteFetchConfigurationServiceHolder {
*/
public static RemoteFetchConfigurationService getRemoteFetchConfigurationService() {

return RemoteFetchConfigurationServiceHolder.SERVICE;
if (log.isDebugEnabled()) {
log.debug("Retrieving RemoteFetchConfigurationService OSGi service.");
}
RemoteFetchConfigurationService service = RemoteFetchConfigurationServiceHolder.SERVICE;
if (service == null) {
log.warn("RemoteFetchConfigurationService OSGi service is not available.");
} else if (log.isDebugEnabled()) {
log.debug("RemoteFetchConfigurationService OSGi service retrieved successfully.");
}
return service;
}
}
Loading