AbstractNotificationOperation.java 6.67 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.ui.components.HtmlResponse
 *  com.day.cq.i18n.I18n
 *  javax.jcr.RepositoryException
 *  javax.servlet.http.HttpServletRequest
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.servlets.post.Modification
 *  org.osgi.framework.BundleContext
 *  org.osgi.framework.ServiceReference
 *  org.osgi.service.component.ComponentContext
 */
package com.adobe.cq.mobile.notifications.impl.operations;

import com.adobe.cq.mobile.notifications.impl.exceptions.NotificationException;
import com.adobe.cq.mobile.notifications.impl.services.NotificationService;
import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.impl.operations.MobileAbstractOperation;
import com.adobe.cq.mobile.platform.impl.operations.MobileOperationException;
import com.adobe.cq.mobile.platform.impl.utils.MobileUtil;
import com.adobe.granite.ui.components.HtmlResponse;
import com.day.cq.i18n.I18n;
import java.util.List;
import javax.jcr.RepositoryException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.servlets.post.Modification;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;

public abstract class AbstractNotificationOperation
extends MobileAbstractOperation {
    public static final String PARAM_APP_INSTANCE = "appInstance";
    public static final String RESPONSE_TITLE = "Service Response";
    private ComponentContext context;
    protected I18n i18n;

    @Override
    protected void perform(SlingHttpServletRequest request, HtmlResponse response, List<Modification> changes) {
        String message;
        String title;
        this.i18n = new I18n((HttpServletRequest)request);
        String appInstanceParam = request.getParameter("appInstance");
        if (StringUtils.isBlank((String)appInstanceParam)) {
            String message2 = this.i18n.get("Missing required parameter {0}", null, new Object[]{"appInstance"});
            String title2 = this.i18n.get("Error");
            this.generateError(response, message2, title2);
            return;
        }
        Resource appInstance = request.getResourceResolver().getResource(appInstanceParam);
        MobileResource mobileResource = (MobileResource)appInstance.adaptTo(MobileResource.class);
        ValueMap properties = null;
        NotificationService notificationService = null;
        try {
            MobileResource shellInstance = MobileUtil.getAppInstance(mobileResource);
            properties = shellInstance.getProperties();
            String pushServiceId = (String)properties.get("rps/pushServiceId", String.class);
            if (pushServiceId == null) {
                String msg = "No push notification service Id found.  It is possible the notification config is invalid or points to a non-existent resource.";
                String message3 = this.i18n.get(msg, null);
                String title3 = this.i18n.get("Error");
                this.generateError(response, message3, title3, new MobileOperationException(msg));
                return;
            }
            String filter = "(cq.mobile.apps.notification.service=" + pushServiceId + ")";
            ServiceReference[] refs = this.context.getBundleContext().getServiceReferences(NotificationService.class.getName(), filter);
            if (refs == null || refs.length == 0) {
                String msg = "No push notification service has been registered for the type '" + pushServiceId + "'";
                String message4 = this.i18n.get(msg, null);
                String title4 = this.i18n.get("Error");
                this.generateError(response, message4, title4, new MobileOperationException(msg));
                return;
            }
            ServiceReference ref = refs[0];
            notificationService = (NotificationService)this.context.getBundleContext().getService(ref);
        }
        catch (Throwable e) {
            message = this.i18n.get("The server is unable to process your request.");
            title = this.i18n.get("Error");
            this.generateError(response, message, title, e);
        }
        try {
            this.performOperation(request, response, mobileResource, notificationService);
        }
        catch (JSONException e) {
            message = this.i18n.get("Exception creating JSON data.");
            title = this.i18n.get("Error");
            this.generateError(response, message, title, (Throwable)e);
        }
        catch (NotificationException e) {
            message = this.i18n.get(e.getMessage());
            title = this.i18n.get("Error");
            Object pushNotificationConfigObj = null;
            if (properties != null) {
                pushNotificationConfigObj = properties.get((Object)"notificationConfig");
            }
            Integer exCode = e.getStatusCode();
            if (pushNotificationConfigObj == null || exCode != 1200 && !notificationService.getConfigRelatedErrorMessages().contains(e.getMessage())) {
                this.generateError(response, message, title, e);
            }
            String url = pushNotificationConfigObj.toString().trim();
            while (url.endsWith("/")) {
                url = url.substring(0, url.length() - 1);
            }
            this.generateError(response, message, title, e, url + ".html", this.i18n.get("Edit Configuration"));
        }
    }

    protected void activate(ComponentContext context) throws RepositoryException {
        this.context = context;
    }

    protected abstract void performOperation(SlingHttpServletRequest var1, HtmlResponse var2, MobileResource var3, NotificationService var4) throws NotificationException, JSONException;

    protected boolean verifyMobileResourceType(HtmlResponse response, MobileResource mobileResource, String type) {
        if (mobileResource == null || !mobileResource.isA(type)) {
            String message = this.i18n.get("Internal Error trying to resolve application type: {0}", null, new Object[]{type});
            String title = this.i18n.get("Error");
            this.generateError(response, message, title);
            return false;
        }
        return true;
    }
}