Search in sources :

Example 81 with InputStream

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;
}
Also used : RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKey(java.security.PrivateKey) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) UnrecoverableKeyException(java.security.UnrecoverableKeyException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PublicKey(java.security.PublicKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey)

Example 82 with InputStream

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();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) Certificate(java.security.cert.Certificate)

Example 83 with InputStream

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();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) FileInfoDTO(org.wso2.carbon.apimgt.rest.api.store.dto.FileInfoDTO)

Example 84 with InputStream

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) LifecycleState(org.wso2.carbon.lcm.core.impl.LifecycleState) LabelDAO(org.wso2.carbon.apimgt.core.dao.LabelDAO) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) APILifecycleManager(org.wso2.carbon.apimgt.core.api.APILifecycleManager) HttpURLConnection(java.net.HttpURLConnection) ByteArrayInputStream(java.io.ByteArrayInputStream) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) KeyManager(org.wso2.carbon.apimgt.core.api.KeyManager) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 85 with InputStream

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;
}
Also used : ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Aggregations

Test (org.testng.annotations.Test)80 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)67 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)58 InputStream (java.io.InputStream)54 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)54 Event (org.wso2.siddhi.core.event.Event)48 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)47 IOException (java.io.IOException)32 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)24 ByteArrayInputStream (java.io.ByteArrayInputStream)20 API (org.wso2.carbon.apimgt.core.models.API)18 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)17 FileInputStream (java.io.FileInputStream)15 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)13 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)12 Response (javax.ws.rs.core.Response)11 HashMap (java.util.HashMap)9 APIMgtWSDLException (org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException)8 File (java.io.File)7 Connection (java.sql.Connection)7