RegistrationInfoHelper.java 3.19 KB
/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2015 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 **************************************************************************/
package libs.screens.dcc.components.registrationcheck;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.tenant.Tenant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.cq.screens.device.registration.PendingDevice;
import com.adobe.cq.screens.device.registration.RegistrationService;
import com.adobe.cq.sightly.WCMUsePojo;

public class RegistrationInfoHelper extends WCMUsePojo {

    /**
     * default logger
     */
    private static final Logger log = LoggerFactory.getLogger(RegistrationInfoHelper.class);

    private String registrationCode;

    private Map<String, String> attributes;

    @Override
    public void activate() throws Exception {
        final RegistrationService registrationService = getSlingScriptHelper().getService(RegistrationService.class);
        final RequestParameter itemParameter = getRequest().getRequestParameter("item");
        final String deviceToken = itemParameter != null ? itemParameter.getString() : null;

        if (deviceToken == null) {
            log.warn("Unable to get registered device. deviceToken is missing as request parameter.");
            return;
        }

        PendingDevice pendingDevice = registrationService.getDevice(deviceToken);
        if (pendingDevice == null) {
            log.warn("Unable to get registered device for {}. no such device.", deviceToken);
            return;
        }

        registrationCode = pendingDevice.getRegistrationCode();
        attributes = new HashMap<String, String>();
        JSONObject metadata = pendingDevice.getMetadata();
        Iterator<String> iter = metadata.keys();
        while(iter.hasNext()) {
            String key = iter.next();
            attributes.put("data-" + key, metadata.getString(key));
        }
    }

    public String getTenant() {
        Tenant tenant = getResourceResolver().adaptTo(Tenant.class);
        if (tenant != null) {
            return tenant.getName();
        }

        String tenantRoot = Text.getAbsoluteParent(getRequest().getRequestPathInfo().getSuffix(), 2);
        return tenantRoot.length() > 0 ? Text.getName(tenantRoot) : null;
    }

    public String getRegistrationCode() {
        return registrationCode;
    }

    public Map<String, String> getAttributes() {
        return attributes;
    }
}