use of org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode in project carbon-apimgt by wso2.
the class EndpointCertificatesApiServiceImpl method addEndpointCertificate.
public Response addEndpointCertificate(InputStream certificateInputStream, Attachment certificateDetail, String alias, String endpoint, MessageContext messageContext) {
try {
if (StringUtils.isEmpty(alias) || StringUtils.isEmpty(endpoint)) {
RestApiUtil.handleBadRequest("The alias and/ or endpoint should not be empty", log);
}
ContentDisposition contentDisposition = certificateDetail.getContentDisposition();
String fileName = contentDisposition.getParameter(RestApiConstants.CONTENT_DISPOSITION_FILENAME);
if (StringUtils.isBlank(fileName)) {
RestApiUtil.handleBadRequest("Certificate update failed. Proper Certificate file should be provided", log);
}
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String userName = RestApiCommonUtil.getLoggedInUsername();
String base64EncodedCert = CertificateRestApiUtils.generateEncodedCertificate(certificateInputStream);
int responseCode = apiProvider.addCertificate(userName, base64EncodedCert, alias, endpoint);
if (log.isDebugEnabled()) {
log.debug(String.format("Add certificate operation response code : %d", responseCode));
}
if (ResponseCode.SUCCESS.getResponseCode() == responseCode) {
CertMetadataDTO certificateDTO = new CertMetadataDTO();
certificateDTO.setEndpoint(endpoint);
certificateDTO.setAlias(alias);
URI createdCertUri = new URI(RestApiConstants.CERTS_BASE_PATH + "?alias=" + alias);
return Response.created(createdCertUri).entity(certificateDTO).build();
} else if (ResponseCode.INTERNAL_SERVER_ERROR.getResponseCode() == responseCode) {
RestApiUtil.handleInternalServerError("Error while adding the certificate due to an" + " internal server error", log);
} else if (ResponseCode.ALIAS_EXISTS_IN_TRUST_STORE.getResponseCode() == responseCode) {
RestApiUtil.handleResourceAlreadyExistsError("The alias '" + alias + "' already exists in the trust store.", log);
} else if (ResponseCode.CERTIFICATE_EXPIRED.getResponseCode() == responseCode) {
RestApiUtil.handleBadRequest("Error while adding the certificate. Certificate Expired.", log);
}
} catch (APIManagementException e) {
RestApiUtil.handleInternalServerError("Error while adding the certificate due to an internal server " + "error", log);
} catch (IOException e) {
RestApiUtil.handleInternalServerError("Error while generating the encoded certificate", log);
} catch (URISyntaxException e) {
RestApiUtil.handleInternalServerError("Error while generating the resource location URI for alias '" + alias + "'", log);
}
return null;
}
use of org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode in project carbon-apimgt by wso2.
the class EndpointCertificatesApiServiceImpl method updateEndpointCertificateByAlias.
public Response updateEndpointCertificateByAlias(String alias, InputStream certificateInputStream, Attachment certificateDetail, MessageContext messageContext) {
try {
if (StringUtils.isEmpty(alias)) {
RestApiUtil.handleBadRequest("The alias should not be empty", log);
}
ContentDisposition contentDisposition = certificateDetail.getContentDisposition();
String fileName = contentDisposition.getParameter(RestApiConstants.CONTENT_DISPOSITION_FILENAME);
if (StringUtils.isBlank(fileName)) {
RestApiUtil.handleBadRequest("Certificate update failed. The Certificate should not be empty", log);
}
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String userName = RestApiCommonUtil.getLoggedInUsername();
int tenantId = APIUtil.getTenantId(userName);
if (!apiProvider.isCertificatePresent(tenantId, alias)) {
if (log.isDebugEnabled()) {
log.debug(String.format("Could not find a certificate in truststore which belongs to tenant : %d " + "and with alias : %s. Hence the operation is terminated.", tenantId, alias));
}
RestApiUtil.handleResourceNotFoundError("Could not update the certificate. " + "The alias '" + alias + "' not found.", log);
}
String base64EncodedCert = CertificateRestApiUtils.generateEncodedCertificate(certificateInputStream);
int responseCode = apiProvider.updateCertificate(base64EncodedCert, alias);
List<CertificateMetadataDTO> updatedCertificate = apiProvider.searchCertificates(tenantId, alias, null);
if (ResponseCode.SUCCESS.getResponseCode() == responseCode && updatedCertificate.size() > 0) {
CertificateMetadataDTO certificateMetadata = updatedCertificate.get(0);
CertMetadataDTO certificateDTO = new CertMetadataDTO();
certificateDTO.setAlias(certificateMetadata.getAlias());
certificateDTO.setEndpoint(certificateMetadata.getEndpoint());
URI updatedCertUri = new URI(RestApiConstants.CERTS_BASE_PATH + "?alias=" + alias);
return Response.ok(updatedCertUri).entity(certificateDTO).build();
}
if (ResponseCode.INTERNAL_SERVER_ERROR.getResponseCode() == responseCode) {
RestApiUtil.handleInternalServerError("Error while updating the certificate due to an internal " + "server error", log);
} else if (ResponseCode.CERTIFICATE_NOT_FOUND.getResponseCode() == responseCode) {
RestApiUtil.handleResourceNotFoundError("", log);
} else if (ResponseCode.CERTIFICATE_EXPIRED.getResponseCode() == responseCode) {
RestApiUtil.handleBadRequest("Error while updating the certificate. Certificate Expired.", log);
}
} catch (APIManagementException e) {
RestApiUtil.handleInternalServerError("Error while adding the certificate due to an internal server " + "error", log);
} catch (IOException e) {
RestApiUtil.handleInternalServerError("Error while encoding certificate", log);
} catch (URISyntaxException e) {
RestApiUtil.handleInternalServerError("Error while generating the resource location URI for alias '" + alias + "'", log);
}
return null;
}
use of org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode in project carbon-apimgt by wso2.
the class APIProviderImpl method deleteCertificate.
@Override
public int deleteCertificate(String userName, String alias, String endpoint) throws APIManagementException {
ResponseCode responseCode = ResponseCode.INTERNAL_SERVER_ERROR;
String tenantDomain = MultitenantUtils.getTenantDomain(userName);
try {
int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
responseCode = certificateManager.deleteCertificateFromParentNode(alias, endpoint, tenantId);
CertificateEvent certificateEvent = new CertificateEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.ENDPOINT_CERTIFICATE_REMOVE.toString(), tenantDomain, alias, endpoint);
APIUtil.sendNotification(certificateEvent, APIConstants.NotifierType.CERTIFICATE.name());
} catch (UserStoreException e) {
handleException("Error while reading tenant information", e);
}
return responseCode.getResponseCode();
}
use of org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode in project carbon-apimgt by wso2.
the class APIProviderImpl method addClientCertificate.
@Override
public int addClientCertificate(String userName, APIIdentifier apiIdentifier, String certificate, String alias, String tierName, String organization) throws APIManagementException {
ResponseCode responseCode = ResponseCode.INTERNAL_SERVER_ERROR;
String tenantDomain = MultitenantUtils.getTenantDomain(userName);
try {
int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
responseCode = certificateManager.addClientCertificate(apiIdentifier, certificate, alias, tierName, tenantId, organization);
} catch (UserStoreException e) {
handleException("Error while reading tenant information, client certificate addition failed for the API " + apiIdentifier.toString(), e);
}
return responseCode.getResponseCode();
}
use of org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode in project carbon-apimgt by wso2.
the class APIProviderImpl method deleteClientCertificate.
@Override
public int deleteClientCertificate(String userName, APIIdentifier apiIdentifier, String alias) throws APIManagementException {
ResponseCode responseCode = ResponseCode.INTERNAL_SERVER_ERROR;
String tenantDomain = MultitenantUtils.getTenantDomain(userName);
try {
int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
responseCode = certificateManager.deleteClientCertificateFromParentNode(apiIdentifier, alias, tenantId);
} catch (UserStoreException e) {
handleException("Error while reading tenant information while trying to delete client certificate with alias " + alias + " for the API " + apiIdentifier.toString(), e);
}
return responseCode.getResponseCode();
}
Aggregations