use of org.wso2.siddhi.query.api.execution.query.input.stream.InputStream in project carbon-apimgt by wso2.
the class JWTWithRSASignatureImpl method getPrivateKey.
/**
* {@inheritDoc}
*/
@Override
public PrivateKey getPrivateKey(String keyStoreFilePath, String keyStorePassword, String alias, String aliasPassword) throws APIManagementException {
if (keyStoreFilePath == null) {
throw new IllegalArgumentException("Path to key store file must not be null");
}
if (keyStorePassword == null) {
throw new IllegalArgumentException("The key store password must not be null");
}
if (alias == null) {
throw new IllegalArgumentException("The Alias must not be null");
}
if (aliasPassword == null) {
throw new IllegalArgumentException("The Alias password not be null");
}
Key key;
try (InputStream inputStream = new FileInputStream(keyStoreFilePath)) {
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(inputStream, keyStorePassword.toCharArray());
key = keyStore.getKey(alias, aliasPassword.toCharArray());
} catch (UnrecoverableKeyException | NoSuchAlgorithmException | KeyStoreException | CertificateException | IOException e) {
throw new APIManagementException("Error getting requested key: Private key not found ", e);
}
if (!(key instanceof PrivateKey)) {
throw new APIManagementException("Error getting requested key: Private key not found ");
}
return (PrivateKey) key;
}
use of org.wso2.siddhi.query.api.execution.query.input.stream.InputStream in project carbon-apimgt by wso2.
the class JWTWithRSASignatureImpl method getPublicKey.
/**
* {@inheritDoc}
*/
@Override
public PublicKey getPublicKey(String keyStoreFilePath, String keyStorePassword, String alias) throws APIManagementException {
if (keyStoreFilePath == null) {
throw new IllegalArgumentException("Path to key store file must not be null");
}
if (keyStorePassword == null) {
throw new IllegalArgumentException("The key store password must not be null");
}
if (alias == null) {
throw new IllegalArgumentException("The Alias must not be null");
}
Certificate cert;
try (FileInputStream inputStream = new FileInputStream(keyStoreFilePath)) {
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(inputStream, keyStorePassword.toCharArray());
cert = keyStore.getCertificate(alias);
} catch (NoSuchAlgorithmException | CertificateException | KeyStoreException | IOException e) {
throw new APIManagementException("Error getting requested key: Public key not found ", e);
}
return cert.getPublicKey();
}
use of org.wso2.siddhi.query.api.execution.query.input.stream.InputStream in project carbon-apimgt by wso2.
the class CompositeApisApiServiceImpl method compositeApisApiIdImplementationPut.
@Override
public Response compositeApisApiIdImplementationPut(String apiId, InputStream apiImplementationInputStream, FileInfo apiImplementationDetail, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIStore apiStore = RestApiUtil.getConsumer(username);
String existingFingerprint = compositeApisApiIdImplementationGetFingerprint(apiId, null, null, request);
if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && ifMatch.contains(existingFingerprint)) {
return Response.notModified().build();
}
apiStore.updateCompositeApiImplementation(apiId, apiImplementationInputStream);
String uriString = RestApiConstants.RESOURCE_PATH_IMPLEMENTATION.replace(RestApiConstants.APIID_PARAM, apiId);
FileInfoDTO infoDTO = new FileInfoDTO();
infoDTO.setRelativePath(uriString);
infoDTO.setMediaType(MediaType.APPLICATION_OCTET_STREAM);
String newFingerprint = compositeApisApiIdImplementationGetFingerprint(apiId, null, null, request);
return Response.ok().header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").entity(infoDTO).build();
} catch (APIManagementException e) {
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.siddhi.query.api.execution.query.input.stream.InputStream in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testAddApiFromDefinitionFromUrlConnection.
@Test(description = "Add api from definition using httpUrlConnection")
public void testAddApiFromDefinitionFromUrlConnection() throws APIManagementException, LifecycleException, IOException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
LabelDAO labelDao = Mockito.mock(LabelDAO.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
HttpURLConnection httpURLConnection = Mockito.mock(HttpURLConnection.class);
Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APIGateway gateway = Mockito.mock(APIGateway.class);
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
KeyManager keyManager = Mockito.mock(KeyManager.class);
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, APIMgtConstants.DEFAULT_API_POLICY)).thenReturn(new APIPolicy(APIMgtConstants.DEFAULT_API_POLICY));
APIPublisherImpl apiPublisher = getApiPublisherImpl(null, keyManager, apiDAO, null, null, policyDAO, apiLifecycleManager, labelDao, null, null, null, gatewaySourceGenerator, gateway);
String def = SampleTestObjectCreator.apiDefinition;
InputStream apiDefinition = new ByteArrayInputStream(def.getBytes());
Mockito.when(httpURLConnection.getInputStream()).thenReturn(apiDefinition);
Mockito.when(httpURLConnection.getResponseCode()).thenReturn(200);
apiPublisher.addApiFromDefinition(httpURLConnection);
Mockito.verify(apiLifecycleManager, Mockito.times(1)).addLifecycle(APIMgtConstants.API_LIFECYCLE, USER);
}
use of org.wso2.siddhi.query.api.execution.query.input.stream.InputStream in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method buildResponseIfParamsInvalid.
/**
* Validate API deefinition import/validate parameters
*
* @param type API definition type (SWAGGER or WSDL)
* @param fileInputStream file content stream
* @param url URL of the definition
* @return Response if any parameter is invalid. Otherwise returns null.
*/
private Response buildResponseIfParamsInvalid(String type, InputStream fileInputStream, String url) {
final String SWAGGER = APIDefinitionValidationResponseDTO.DefinitionTypeEnum.SWAGGER.toString();
final String WSDL = APIDefinitionValidationResponseDTO.DefinitionTypeEnum.WSDL.toString();
if (!SWAGGER.equals(type) && !WSDL.equals(type)) {
String errorMessage = "Unsupported definition type. Only SWAGGER or WSDL is allowed";
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(ExceptionCodes.UNSUPPORTED_API_DEFINITION_TYPE);
log.error(errorMessage);
return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
}
if (url == null && fileInputStream == null) {
String msg = "Either 'file' or 'url' should be specified";
log.error(msg);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900700L, msg);
return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
}
if (fileInputStream != null && url != null) {
String msg = "Only one of 'file' and 'url' should be specified";
log.error(msg);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900700L, msg);
return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
}
return null;
}
Aggregations