Search in sources :

Example 51 with Label

use of org.wso2.carbon.apimgt.api.model.Label in project carbon-business-process by wso2.

the class HTRenderingApiImpl method getRenderingInputElements.

/**
 * @param taskIdentifier : interested task identifier
 * @return set of input renderings wrapped within InputType
 * @throws IllegalArgumentFault : error occured while retrieving renderings from task definition
 * @throws IOException          If an error occurred while reading from the input source
 * @throws SAXException         If the content in the input source is invalid
 */
private InputType getRenderingInputElements(URI taskIdentifier) throws IllegalArgumentFault, IOException, SAXException, IllegalOperationFault, IllegalAccessFault, IllegalStateFault, XPathExpressionException {
    // TODO Chaching : check cache against task id for input renderings
    QName renderingType = new QName(htRenderingNS, "input", "wso2");
    String inputRenderings = (String) taskOps.getRendering(taskIdentifier, renderingType);
    // Create input element
    InputType renderingInputs = null;
    // check availability of input renderings
    if (inputRenderings != null && inputRenderings.length() > 0) {
        // parse input renderings
        Element inputRenderingsElement = DOMUtils.stringToDOM(inputRenderings);
        // retrieve input elements
        NodeList inputElementList = inputRenderingsElement.getElementsByTagNameNS(htRenderingNS, "element");
        Element taskInputMsgElement = DOMUtils.stringToDOM((String) taskOps.getInput(taskIdentifier, null));
        if (inputElementList != null && inputElementList.getLength() > 0) {
            // hold number of input element
            int inputElementNum = inputElementList.getLength();
            InputElementType[] inputElements = new InputElementType[inputElementNum];
            for (int i = 0; i < inputElementNum; i++) {
                Element tempElement = (Element) inputElementList.item(i);
                String label = tempElement.getElementsByTagNameNS(htRenderingNS, "label").item(0).getTextContent();
                String value = tempElement.getElementsByTagNameNS(htRenderingNS, "value").item(0).getTextContent();
                // if the value starts with '/' then considered as an xpath and evaluate over received Input Message
                if (value.startsWith("/") && taskInputMsgElement != null) {
                    // value represents xpath. evaluate against the input message
                    String xpathValue = evaluateXPath(value, taskInputMsgElement, inputRenderingsElement.getOwnerDocument());
                    if (xpathValue != null) {
                        value = xpathValue;
                    }
                }
                inputElements[i] = new InputElementType();
                inputElements[i].setLabel(label);
                inputElements[i].setValue(value);
            }
            renderingInputs = new InputType();
            renderingInputs.setElement(inputElements);
        // TODO cache renderingInputs against task instance id
        }
    }
    return renderingInputs;
}
Also used : InputElementType(org.wso2.carbon.humantask.rendering.api.InputElementType) InputType(org.wso2.carbon.humantask.rendering.api.InputType) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList)

Example 52 with Label

use of org.wso2.carbon.apimgt.api.model.Label in project carbon-apimgt by wso2.

the class DBRetriever method retrieveAllArtifacts.

@Override
public List<String> retrieveAllArtifacts(String label, String tenantDomain) throws ArtifactSynchronizerException {
    List<String> gatewayRuntimeArtifactsArray = new ArrayList<>();
    try {
        String endcodedgatewayLabel = URLEncoder.encode(label, APIConstants.DigestAuthConstants.CHARSET);
        String path = APIConstants.GatewayArtifactSynchronizer.GATEAY_SYNAPSE_ARTIFACTS + "?gatewayLabel=" + endcodedgatewayLabel + "&type=Synapse";
        String endpoint = baseURL + path;
        try (CloseableHttpResponse httpResponse = invokeService(endpoint, tenantDomain)) {
            JSONArray jsonArray = retrieveArtifact(httpResponse);
            if (jsonArray != null) {
                for (int i = 0; i < jsonArray.length(); i++) {
                    gatewayRuntimeArtifactsArray.add(jsonArray.getString(i));
                }
            }
        }
        return gatewayRuntimeArtifactsArray;
    } catch (IOException e) {
        String msg = "Error while executing the http client";
        log.error(msg, e);
        throw new ArtifactSynchronizerException(msg, e);
    }
}
Also used : ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) ArrayList(java.util.ArrayList) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) JSONArray(org.json.JSONArray) IOException(java.io.IOException)

Example 53 with Label

use of org.wso2.carbon.apimgt.api.model.Label in project carbon-apimgt by wso2.

the class DBRetriever method retrieveAttributes.

@Override
public Map<String, String> retrieveAttributes(String apiName, String version, String tenantDomain) throws ArtifactSynchronizerException {
    CloseableHttpResponse httpResponse = null;
    try {
        String endcodedVersion = URLEncoder.encode(version, APIConstants.DigestAuthConstants.CHARSET);
        String path = APIConstants.GatewayArtifactSynchronizer.SYNAPSE_ATTRIBUTES + "?apiName=" + apiName + "&tenantDomain=" + tenantDomain + "&version=" + endcodedVersion;
        String endpoint = baseURL + path;
        httpResponse = invokeService(endpoint, tenantDomain);
        String responseString;
        if (httpResponse.getEntity() != null) {
            responseString = EntityUtils.toString(httpResponse.getEntity(), APIConstants.DigestAuthConstants.CHARSET);
            httpResponse.close();
        } else {
            throw new ArtifactSynchronizerException("HTTP response is empty");
        }
        Map<String, String> apiAttribute = new HashMap<>();
        JSONObject artifactObject = new JSONObject(responseString);
        String label = null;
        String apiId = null;
        try {
            apiId = (String) artifactObject.get(APIConstants.GatewayArtifactSynchronizer.API_ID);
            String labelsStr = artifactObject.get(APIConstants.GatewayArtifactSynchronizer.LABELS).toString();
            Set<String> labelsSet = new Gson().fromJson(labelsStr, new TypeToken<HashSet<String>>() {
            }.getType());
            Set<String> gatewaySubscribedLabel = gatewayArtifactSynchronizerProperties.getGatewayLabels();
            if (!labelsSet.isEmpty() || !gatewaySubscribedLabel.isEmpty()) {
                labelsSet.retainAll(gatewaySubscribedLabel);
                if (!labelsSet.isEmpty()) {
                    label = labelsSet.iterator().next();
                }
            }
        } catch (ClassCastException e) {
            log.error("Unexpected response received from the storage." + e.getMessage());
        }
        apiAttribute.put(APIConstants.GatewayArtifactSynchronizer.API_ID, apiId);
        apiAttribute.put(APIConstants.GatewayArtifactSynchronizer.LABEL, label);
        return apiAttribute;
    } catch (IOException e) {
        String msg = "Error while executing the http client";
        log.error(msg, e);
        throw new ArtifactSynchronizerException(msg, e);
    }
}
Also used : JSONObject(org.json.JSONObject) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) HashMap(java.util.HashMap) TypeToken(com.google.gson.reflect.TypeToken) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Gson(com.google.gson.Gson) IOException(java.io.IOException)

Example 54 with Label

use of org.wso2.carbon.apimgt.api.model.Label in project carbon-apimgt by wso2.

the class RegistryPersistenceUtilTestCase method testcreateAPIArtifactContent.

@Test
public void testcreateAPIArtifactContent() throws APIPersistenceException, APIManagementException, RegistryException {
    API api = new API(new APIIdentifier("pubuser", "TestAPI", "1.0"));
    Set<Tier> availableTiers = new HashSet<Tier>();
    availableTiers.add(new Tier("Unlimited"));
    availableTiers.add(new Tier("Gold"));
    api.setAvailableTiers(availableTiers);
    Set<URITemplate> uriTemplates = new HashSet<URITemplate>();
    URITemplate template = new URITemplate();
    template.setHTTPVerb("GET");
    template.setUriTemplate("/test");
    template.setAuthType("None");
    uriTemplates.add(template);
    api.setUriTemplates(uriTemplates);
    List<APICategory> categories = new ArrayList<APICategory>();
    APICategory category = new APICategory();
    category.setName("testcategory");
    categories.add(category);
    api.setApiCategories(categories);
    List<Label> gatewayLabels = new ArrayList<Label>();
    Label label = new Label();
    label.setName("TestLabel");
    gatewayLabels.add(label);
    GenericArtifact genericArtifact = new GenericArtifactImpl(new QName("", "TestAPI", ""), "application/vnd.wso2-api+xml");
    genericArtifact.setAttribute("URITemplate", "/test");
    GenericArtifact retArtifact = RegistryPersistenceUtil.createAPIArtifactContent(genericArtifact, api);
    Assert.assertEquals("API name does not match", api.getId().getApiName(), retArtifact.getAttribute("overview_name"));
    Assert.assertEquals("API version does not match", api.getId().getVersion(), retArtifact.getAttribute("overview_version"));
    Assert.assertEquals("API provider does not match", api.getId().getProviderName(), retArtifact.getAttribute("overview_provider"));
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) Tier(org.wso2.carbon.apimgt.api.model.Tier) QName(javax.xml.namespace.QName) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) ArrayList(java.util.ArrayList) Label(org.wso2.carbon.apimgt.api.model.Label) GenericArtifactImpl(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifactImpl) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APICategory(org.wso2.carbon.apimgt.api.model.APICategory) HashSet(java.util.HashSet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 55 with Label

use of org.wso2.carbon.apimgt.api.model.Label in project carbon-apimgt by wso2.

the class GatewayArtifactsMgtDAO method retrieveGatewayArtifacts.

public List<APIRuntimeArtifactDto> retrieveGatewayArtifacts(String tenantDomain) throws APIManagementException {
    String query = SQLConstants.RETRIEVE_ARTIFACTS;
    List<APIRuntimeArtifactDto> apiRuntimeArtifactDtoList = new ArrayList<>();
    try (Connection connection = GatewayArtifactsMgtDBUtil.getArtifactSynchronizerConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(query)) {
        preparedStatement.setString(1, tenantDomain);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                String apiId = resultSet.getString("API_ID");
                String label = resultSet.getString("LABEL");
                try {
                    APIRuntimeArtifactDto apiRuntimeArtifactDto = new APIRuntimeArtifactDto();
                    apiRuntimeArtifactDto.setTenantDomain(resultSet.getString("TENANT_DOMAIN"));
                    apiRuntimeArtifactDto.setApiId(apiId);
                    String resolvedVhost = VHostUtils.resolveIfNullToDefaultVhost(label, resultSet.getString("VHOST"));
                    apiRuntimeArtifactDto.setLabel(label);
                    apiRuntimeArtifactDto.setVhost(resolvedVhost);
                    apiRuntimeArtifactDto.setName(resultSet.getString("API_NAME"));
                    apiRuntimeArtifactDto.setVersion(resultSet.getString("API_VERSION"));
                    apiRuntimeArtifactDto.setProvider(resultSet.getString("API_PROVIDER"));
                    apiRuntimeArtifactDto.setRevision(resultSet.getString("REVISION_ID"));
                    apiRuntimeArtifactDto.setType(resultSet.getString("API_TYPE"));
                    apiRuntimeArtifactDto.setContext(resultSet.getString("CONTEXT"));
                    InputStream artifact = resultSet.getBinaryStream("ARTIFACT");
                    if (artifact != null) {
                        byte[] artifactByte = APIMgtDBUtil.getBytesFromInputStream(artifact);
                        try (InputStream newArtifact = new ByteArrayInputStream(artifactByte)) {
                            apiRuntimeArtifactDto.setArtifact(newArtifact);
                        }
                    }
                    apiRuntimeArtifactDto.setFile(true);
                    apiRuntimeArtifactDtoList.add(apiRuntimeArtifactDto);
                } catch (APIManagementException e) {
                    // handle exception inside the loop and continue with other API artifacts
                    log.error(String.format("Error resolving vhost while retrieving runtime artifact for API %s, " + "gateway environment \"%s\", tenant: \"%s\"." + "Skipping runtime artifact for the API.", apiId, label, tenantDomain), e);
                } catch (IOException e) {
                    // handle exception inside the loop and continue with other API artifacts
                    log.error(String.format("Error occurred retrieving input stream from byte array of " + "API: %s, gateway environment \"%s\", tenant: \"%s\".", apiId, label, tenantDomain), e);
                } catch (SQLException e) {
                    // handle exception inside the loop and continue with other API artifacts
                    log.error(String.format("Failed to retrieve Gateway Artifact of API: %s, " + "gateway environment \"%s\", tenant: \"%s\".", apiId, label, tenantDomain), e);
                }
            }
        }
    } catch (SQLException e) {
        handleException("Failed to retrieve Gateway Artifacts.", e);
    }
    return apiRuntimeArtifactDtoList;
}
Also used : SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ByteArrayInputStream(java.io.ByteArrayInputStream) ResultSet(java.sql.ResultSet)

Aggregations

Label (org.wso2.carbon.apimgt.core.models.Label)65 ArrayList (java.util.ArrayList)60 Test (org.testng.annotations.Test)45 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)32 API (org.wso2.carbon.apimgt.core.models.API)29 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)28 SQLException (java.sql.SQLException)26 PreparedStatement (java.sql.PreparedStatement)25 Connection (java.sql.Connection)20 HashMap (java.util.HashMap)18 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)14 Test (org.junit.Test)13 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)13 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)11 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)11 ResultSet (java.sql.ResultSet)10 IOException (java.io.IOException)9 Map (java.util.Map)9 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)9 BeforeTest (org.testng.annotations.BeforeTest)9