Package org.wso2.carbon.registry.app.targets

Examples of org.wso2.carbon.registry.app.targets.ResponseTarget


* TODO: class description
*/
public final class Util {

    public static void checkAccess(String ownerTenantDomain, int ownerTenantId) {
        CarbonContext carbonContext = getCarbonContext();
        String callerTenantDomain = carbonContext.getTenantDomain();
        if (callerTenantDomain == null) {
            throw new IllegalStateException("Caller tenant domain cannot be null");
        }
        int callerTenantId = carbonContext.getTenantId();
        if (callerTenantId == MultitenantConstants.INVALID_TENANT_ID) {
            throw new IllegalStateException("Caller Tenant ID cannot be " + MultitenantConstants.INVALID_TENANT_ID);
        }

        if (callerTenantDomain.equals(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME) &&
View Full Code Here


                                        "],[" + callerTenantId + "]}");
        }
    }

    private static CarbonContext getCarbonContext() {
        CarbonContext carbonContext = CarbonContext.getThreadLocalCarbonContext();
        if (carbonContext == null) {
            throw new IllegalStateException("CarbonContext cannot be null");
        }
        return carbonContext;
    }
View Full Code Here

            PrivilegedCarbonContext.endTenantFlow();
        }
    }

    private void setTenantCredentialsInCarbonContext() {
        PrivilegedCarbonContext cc = PrivilegedCarbonContext.getThreadLocalCarbonContext();
        cc.setTenantId(ownerTenantId);
        cc.setTenantDomain(ownerTenantDomain);
    }
View Full Code Here

        System.setProperty("carbon.home", new File(".").getAbsolutePath());

        String cacheName = "sampleCache";
        // CacheManager cacheManager = Caching.getCacheManager(); // same as Caching.getCacheManagerFactory().getCacheManager("__default__");

        PrivilegedCarbonContext cc = PrivilegedCarbonContext.getThreadLocalCarbonContext();
        cc.setTenantDomain("foo.com");
        cc.setTenantId(1);

        CacheManager cacheManager = Caching.getCacheManagerFactory().getCacheManager("test");
        cache = cacheManager.getCache(cacheName);
    }
View Full Code Here

                uri = uri.substring(0, uri.lastIndexOf("?"));
            } catch (RegistryException e) {
                final StringResponseContext response =
                        new StringResponseContext("Unauthorized", 401);
                response.setHeader("WWW-Authenticate", "Basic realm=\"WSO2-Registry\"");
                return new ResponseTarget(context, response);
            }
        }

        if (registry == null) {
            // Set up secure registry instance
            String authorizationString = request.getAuthorization();
            if (authorizationString != null) {
                // splitting the Authorization string "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
                String values[] = authorizationString.split("\\ ");
                if (values == null || values.length == 0) {
                    final StringResponseContext response =
                            new StringResponseContext("Unauthorized", 401);
                    response.setHeader("WWW-Authenticate", "Basic realm=\"WSO2-Registry\"");
                    return new ResponseTarget(context, response);
                } else if ("Basic".equals(values[0])) {
                    try {
                        // Decode username/password
                        authorizationString = new String(Base64.decode(values[1]));
                        values = authorizationString.split("\\:");
                        String userName = values[0];
                        String password = values[1];
                        String tenantDomain =
                                (String) ((ServletRequestContext) request).getRequest().
                                        getAttribute(MultitenantConstants.TENANT_DOMAIN);
                        int tenantId;
                        String userNameAlong;
                        if (tenantDomain == null) {
                            tenantId = getTenantId(userName);
                            userNameAlong = getUserName(userName);
                        } else {
                            tenantId = getTenantIdFromDomain(tenantDomain);
                            userNameAlong = userName;
                        }
                        registry = embeddedRegistryService.getRegistry(userNameAlong,
                                password, tenantId);
                    } catch (Exception e) {
                        final StringResponseContext response =
                                new StringResponseContext("Unauthorized", 401);
                        response.setHeader("WWW-Authenticate", "Basic realm=\"WSO2-Registry\"");
                        return new ResponseTarget(context, response);
                    }
                } else {
                    // TODO - return an ExceptionTarget which contains the authentication problem
                    // return new ExceptionTarget(400, "Only basic authentication is supported!");
                    return null;
                }
            } else {
                String tenantDomain = (String) requestContext.getRequest().
                        getAttribute(MultitenantConstants.TENANT_DOMAIN);
                int calledTenantId = 0;
                if (tenantDomain != null) {
                    if (RegistryContext.getBaseInstance().getRealmService() == null) {
                        String msg = "Error in getting the tenant manager. " +
                                "The realm service is not available.";
                        log.error(msg);
                        return new ResponseTarget(context, new EmptyResponseContext(400, msg));
                    }
                    TenantManager tenantManager =
                            RegistryContext.getBaseInstance().getRealmService().
                                    getTenantManager();
                    try {
                        calledTenantId = tenantManager.getTenantId(tenantDomain);
                    } catch (org.wso2.carbon.user.api.UserStoreException e) {
                        String msg =
                                "Error in converting tenant domain to the id for tenant domain: " +
                                        tenantDomain + ".";
                        log.error(msg, e);
                        return new ResponseTarget(context, new EmptyResponseContext(400, msg));
                    }
                    try {
                        if (!tenantManager.isTenantActive(calledTenantId)) {
                            // the tenant is not active.
                            String msg =
                                    "The tenant is not active. tenant domain: " + tenantDomain +
                                            ".";
                            log.error(msg);
                            return new ResponseTarget(context, new EmptyResponseContext(400, msg));
                        }
                    } catch (org.wso2.carbon.user.api.UserStoreException e) {
                        String msg =
                                "Error in converting tenant domain to the id for tenant domain: " +
                                        tenantDomain + ".";
                        log.error(msg, e);
                        return new ResponseTarget(context, new EmptyResponseContext(400, msg));
                    }
                    RegistryContext.getBaseInstance().
                            getRealmService().getBootstrapRealmConfiguration();

                }
                String anonUser = CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME;
                try {
                    registry = embeddedRegistryService.getRegistry(anonUser, calledTenantId);
                } catch (RegistryException e) {
                    String msg = "Error in creating the registry.";
                    log.error(msg, e);
                    return new ResponseTarget(context, new EmptyResponseContext(400, msg));
                }
            }
        }

        // Squirrel this away so the adapter can get it later (after all that work we just did!)
        context.setAttribute("userRegistry", registry);

        final String method = context.getMethod();

        /*
        Following code moved further down
        try {
            uri = URLDecoder.decode(uri, "utf-8");
        } catch (UnsupportedEncodingException e) {
            log.error(e);
            return null;
        } */

        if (uri.startsWith(RegistryConstants.PATH_SEPARATOR
                + RegistryConstants.REGISTRY_INSTANCE
                + RegistryConstants.PATH_SEPARATOR
                + RegistryConstants.REGISTRY_INSTANCE)) {
            //if ROOT war is renamed to 'registry', uri will look like following,
            //'/registry/registry/foo/bar'
            //Hence,we need to remove the first 'registry'
            uri = uri.replaceFirst(RegistryConstants.PATH_SEPARATOR
                    + RegistryConstants.REGISTRY_INSTANCE, "");
        }

        // URI will start with the baseURI, which we need to strip off.

        String[] excludeStartArr = {basePath + APPConstants.ATOM,
                basePath + APPConstants.RESOURCE, basePath + "/tags"};

        if (basePath == null) {
            log.error("Base path is null. Aborting the operation.");
            final StringResponseContext response =
                    new StringResponseContext("Internal Server Error", 500);
            return new ResponseTarget(context, response);

        } else if (!basePath.equals("")) {
            for (String excludeStartStr : excludeStartArr) {
                // URI will start with the baseURI, which we need to strip off.
                if (uri.indexOf(excludeStartStr) > -1 &&
                        uri.length() > uri.indexOf(excludeStartStr) + basePath.length()) {
                    uri = uri.substring(uri.indexOf(excludeStartStr) + basePath.length());
                    break;
                }
            }
        }


        if (!uri.startsWith(
                RegistryConstants.PATH_SEPARATOR + RegistryConstants.REGISTRY_INSTANCE)) {
            uri = uri.substring(uri.indexOf(RegistryConstants.PATH_SEPARATOR));
        }
        context.setAttribute("pathInfo", uri);
        String[] parts = splitPath(uri); // splits with "\;"
        boolean hasColon = false;
        TargetType type = null;

        // Let's just see if this is an import first - in which case we can just send it
        // on through.

        if (parts.length > 1) {
            String discriminator = parts[1];

            // If this is a version request, don't do anything special.  Otherwise process.
            if (discriminator.startsWith("version:")) {
                if (parts.length > 2) {
                    // Make sure this is a restore.
                    if (parts[2].equals("restore")) {
                        type = RESTORE_TYPE;
                        uri = parts[0] + RegistryConstants.URL_SEPARATOR + parts[1];
                    } else if (parts[2].equals(APPConstants.ASSOCIATIONS)) {
                        type = ASSOCIATIONS_TYPE;
                        uri = parts[0] + RegistryConstants.URL_SEPARATOR + parts[1];
                    } else {
                        // There's an extra semicolon here somewhere.
                        return null;
                    }
                }
            } else {
                // Store the split URL for later
                context.setAttribute("splitPath", parts);
                int idx = discriminator.indexOf("?");
                if (idx > -1) {
                    discriminator = discriminator.substring(0, idx);
                }

                String suffix = null;
                idx = discriminator.indexOf(":");
                if (idx > -1) {
                    suffix = discriminator.substring(idx + 1, discriminator.length());
                    discriminator = discriminator.substring(0, idx);
                    hasColon = true;
                }

                if (discriminator.startsWith("aspect")) {
                    type = ASPECT_TYPE;
                } else {
                    type = types.get(discriminator);
                }

                if (discriminator.equals("tag") && method.equals("DELETE") && hasColon) {
                    context.setAttribute("tagName", suffix);
                    type = DELETE_TYPE;
                } else if (discriminator.equals("comment") && method.equals("DELETE") && hasColon) {
                    context.setAttribute("commentId", suffix);
                    type = DELETE_TYPE;
                }

                // If we have a discriminator that we don't understand, return a 404
                if (type == null) {
                    return null;
                }

                // For the rest of this code, we'll want the "raw" resource URI
                if (!hasColon || !(type.equals(COMMENTS_TYPE) || type.equals(RATINGS_TYPE) ||
                        type.equals(TAGS_TYPE))) {
                    uri = parts[0];
                }
                if (hasColon && (type.equals(RATINGS_TYPE) || type.equals(TAGS_TYPE))) {
                    type = null;
                }
            }
        }

        int idx = uri.indexOf("?");
        if (idx > -1) {
            String queryString = uri.substring(idx + 1, uri.length());
            context.setAttribute("queryString", queryString);
            uri = uri.substring(0, idx);
        }

        try {
            uri = URLDecoder.decode(uri, "utf-8");
        } catch (UnsupportedEncodingException e) {
            log.error(e);
            return null;
        }

        boolean isMedia = false;
        if (uri.startsWith(APPConstants.RESOURCE)) {
            uri = uri.substring(APPConstants.RESOURCE.length());
            isMedia = true;
        } else if (uri.startsWith(APPConstants.ATOM)) {
            uri = uri.substring(APPConstants.ATOM.length());
        } else if (uri.startsWith("/tags") && (uri.length() == 5 || uri.charAt(5) == '/')) {
            return new SimpleTarget(TAG_URL_TYPE, context);
        } else {
            return null;
        }

        if (uri.length() == 0) {
            uri = "/";
        }

        // See if we're asking for a paginated collection
        String startParam = context.getParameter("start");
        String pageLenParam = context.getParameter("pageLen");
        int start = (startParam == null) ? -1 : Integer.parseInt(startParam);
        int pageLen = (pageLenParam == null) ? -1 : Integer.parseInt(pageLenParam);

        Resource resource = null;
        if (type != null && type.equals(DUMP_TYPE) &&
                method != null && method.equals("POST")) {
            // for restoring a dump we don't need to have available resource
            // here we will create a fake resource to store the path

            resource = new ResourceImpl();
            ((ResourceImpl) resource).setPath(uri);
        } else {
            // in a restore, path don't need to exist.
            try {
                CurrentSession.setUserRealm(registry.getUserRealm());
                CurrentSession.setUser(registry.getUserName());
                if (!AuthorizationUtils.authorize(RegistryUtils.getAbsolutePath(
                        registry.getRegistryContext(), uri),
                        ActionConstants.GET)) {
                    final StringResponseContext response =
                            new StringResponseContext("Unauthorized", 401);
                    response.setHeader("WWW-Authenticate", "Basic realm=\"WSO2-Registry\"");
                    return new ResponseTarget(context, response);
                } else if (start > -1 || pageLen > -1) {
                    resource = registry.get(uri, start, pageLen);
                } else {
                    resource = registry.get(uri);
                }
            } catch (AuthorizationFailedException e) {
                final StringResponseContext response =
                        new StringResponseContext("Unauthorized", 401);
                response.setHeader("WWW-Authenticate", "Basic realm=\"WSO2-Registry\"");
                return new ResponseTarget(context, response);
            } catch (ResourceNotFoundException e) {
                // If this is a straight-ahead POST to a non-existent directory, create it?
                if (method.equals("POST") && parts.length == 1) {
                    // Need to create it.
                    try {
                        Collection c = registry.newCollection();
                        registry.put(uri, c);
                        resource = registry.get(uri);
                    } catch (RegistryException e1) {
                        log.error(e1);
                        return null;
                    }
                }
                if (resource == null) {
                    return null;
                }
            } catch (RegistryException e) {
                return null; // return 404
            }

            if (method.equals("GET")) {
                // eTag based conditional get
                String ifNonMatchValue = context.getHeader("if-none-match");
                if (ifNonMatchValue != null) {
                    String currentETag = Utils.calculateEntityTag(resource);
                    if (ifNonMatchValue.equals(currentETag)) {
                        /* the version is not modified */
                        ResponseContext response = new StringResponseContext("Not Modified", 304);
                        return new ResponseTarget(context, response);
                    }
                }

                // date based conditional get
                long ifModifiedSinceValue = 0;
                Date ifModifiedSince = context.getDateHeader("If-Modified-Since");
                if (ifModifiedSince != null) {
                    ifModifiedSinceValue = ifModifiedSince.getTime();
                }

                if (ifModifiedSinceValue > 0) {
                    long lastModifiedValue = resource.getLastModified().getTime();
                    // convert the time values from milliseconds to seconds
                    ifModifiedSinceValue /= 1000;
                    lastModifiedValue /= 1000;

                    /* condition to check we have latest updates in terms of dates */
                    if (ifModifiedSinceValue >= lastModifiedValue) {
                        /* no need to response with data */
                        ResponseContext response = new StringResponseContext("Not Modified", 304);
                        return new ResponseTarget(context, response);
                    }
                }
            }
        }
        context.setAttribute("MyResolver", this);
View Full Code Here

        }

        public void run() {
            try {
                MessageContext msgCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
                Event<MessageContext> event = new Event(msgCtx);
                subscriptions = subscriptionManager.getMatchingSubscriptions(event);
            } catch (EventException e) {
                handleException("Matching subscriptions fetching error", e);
            }
View Full Code Here

     * @throws EventException event
     */
    private void processGetStatusRequest(MessageContext mc,
                                         ResponseMessageBuilder messageBuilder)
            throws AxisFault, EventException {
        Subscription subscription =
                SubscriptionMessageBuilder.createGetStatusMessage(mc);
        if (log.isDebugEnabled()) {
            log.debug("GetStatus request recived for SynapseSubscription ID : " +
                    subscription.getId());
        }
        subscription = subscriptionManager.getSubscription(subscription.getId());
        if (subscription != null) {
            if (log.isDebugEnabled()) {
                log.debug("Sending GetStatus responce for SynapseSubscription ID : " +
                        subscription.getId());
            }
            //send the responce
            SOAPEnvelope soapEnvelope = messageBuilder.genGetStatusResponse(subscription);
            dispatchResponse(soapEnvelope, EventingConstants.WSE_GET_STATUS_RESPONSE,
                    mc, false);
View Full Code Here

            // Adding static subscriptions
            List<Subscription> staticSubscriptionList =
                    eventSource.getSubscriptionManager().getStaticSubscriptions();
            for (Iterator<Subscription> iterator = staticSubscriptionList.iterator();
                 iterator.hasNext();) {
                Subscription staticSubscription = iterator.next();
                OMElement staticSubElem =
                        fac.createOMElement("subscription", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
                staticSubElem.addAttribute(
                        fac.createOMAttribute("id", nullNS, staticSubscription.getId()));
                OMElement filterElem =
                        fac.createOMElement("filter", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
                filterElem.addAttribute(fac.createOMAttribute("source", nullNS,
                        (String) staticSubscription.getFilterValue()));
                filterElem.addAttribute(fac.createOMAttribute("dialect", nullNS,
                        (String) staticSubscription.getFilterDialect()));
                staticSubElem.addChild(filterElem);
                OMElement endpointElem =
                        fac.createOMElement("endpoint", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
                OMElement addressElem =
                        fac.createOMElement("address", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
                addressElem.addAttribute(
                        fac.createOMAttribute("uri", nullNS, staticSubscription.getEndpointUrl()));
                endpointElem.addChild(addressElem);
                staticSubElem.addChild(endpointElem);
                if (staticSubscription.getExpires() != null) {
                    OMElement expiresElem =
                            fac.createOMElement("expires", XMLConfigConstants.SYNAPSE_OMNAMESPACE);
                    fac.createOMText(expiresElem,
                            ConverterUtil.convertToString(staticSubscription.getExpires()));
                    staticSubElem.addChild(expiresElem);
                }
                evenSourceElem.addChild(staticSubElem);
            }
View Full Code Here


    public SynapseSubscription() {
        this.setId(UIDGenerator.generateURNString());
        this.setDeliveryMode(EventingConstants.WSE_DEFAULT_DELIVERY_MODE);
        SubscriptionData subscriptionData = new SubscriptionData();
        subscriptionData.setProperty(SynapseEventingConstants.STATIC_ENTRY, "false");
        this.setSubscriptionData(subscriptionData);
    }
View Full Code Here

                    .getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "class"));
            if (clazz != null) {
                String className = clazz.getAttributeValue();
                try {
                    Class subscriptionManagerClass = Class.forName(className);
                    SubscriptionManager manager =
                            (SubscriptionManager) subscriptionManagerClass.newInstance();
                    Iterator itr = subscriptionManagerElem.getChildrenWithName(PROPERTIES_QNAME);
                    while (itr.hasNext()) {
                        OMElement propElem = (OMElement) itr.next();
                        String propName =
                                propElem.getAttribute(new QName("name")).getAttributeValue();
                        String propValue =
                                propElem.getAttribute(new QName("value")).getAttributeValue();
                        if (propName != null && !"".equals(propName.trim()) &&
                                propValue != null && !"".equals(propValue.trim())) {

                            propName = propName.trim();
                            propValue = propValue.trim();

                            PasswordManager passwordManager =
                                    PasswordManager.getInstance();
                            String key = eventSource.getName() + "." + propName;

                            if (passwordManager.isInitialized()
                                    && passwordManager.isTokenProtected(key)) {
                                eventSource.putConfigurationProperty(propName, propValue);
                                propValue = passwordManager.resolve(propValue);
                            }

                            manager.addProperty(propName, propValue);
                        }
                    }
                    eventSource.setSubscriptionManager(manager);
                    eventSource.getSubscriptionManager()
                            .init(); // Initialise before doing further processing, required for static subscriptions
View Full Code Here

TOP

Related Classes of org.wso2.carbon.registry.app.targets.ResponseTarget

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.