SendNotificationOperation.java 3.44 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.ui.components.HtmlResponse
 *  com.day.cq.i18n.I18n
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.http.client.HttpClient
 *  org.apache.http.impl.client.CloseableHttpClient
 *  org.apache.http.impl.client.HttpClientBuilder
 *  org.apache.http.osgi.services.HttpClientBuilderFactory
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 */
package com.adobe.cq.mobile.notifications.impl.operations;

import com.adobe.cq.mobile.notifications.impl.exceptions.NotificationException;
import com.adobe.cq.mobile.notifications.impl.operations.AbstractNotificationOperation;
import com.adobe.cq.mobile.notifications.impl.services.NotificationService;
import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceType;
import com.adobe.granite.ui.components.HtmlResponse;
import com.day.cq.i18n.I18n;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.osgi.services.HttpClientBuilderFactory;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;

@Component(metatype=0, label="Send Push Notification Operation")
@Service
@Properties(value={@Property(name="sling.post.operation", value={"mobileapps:sendNotification"})})
public class SendNotificationOperation
extends AbstractNotificationOperation {
    public static final String OPERATION_NAME = "sendNotification";
    @Reference
    private HttpClientBuilderFactory httpClientBuilderFactory;

    @Override
    protected void performOperation(SlingHttpServletRequest request, HtmlResponse response, MobileResource mobileResource, NotificationService notificationService) throws NotificationException, JSONException {
        if (!this.verifyMobileResourceType(response, mobileResource, MobileResourceType.NOTIFICATION.getType())) {
            return;
        }
        CloseableHttpClient httpclient = this.httpClientBuilderFactory.newBuilder().build();
        JSONObject sendResult = notificationService.send(mobileResource, (HttpClient)httpclient);
        JSONObject jsonResponse = new JSONObject();
        jsonResponse.put("response", (Object)sendResult);
        jsonResponse.put("status", (Object)"ok");
        this.generateResponse(response, response.getStatusCode(), jsonResponse.toString(), this.i18n.get("Service Response"));
    }

    protected void bindHttpClientBuilderFactory(HttpClientBuilderFactory httpClientBuilderFactory) {
        this.httpClientBuilderFactory = httpClientBuilderFactory;
    }

    protected void unbindHttpClientBuilderFactory(HttpClientBuilderFactory httpClientBuilderFactory) {
        if (this.httpClientBuilderFactory == httpClientBuilderFactory) {
            this.httpClientBuilderFactory = null;
        }
    }
}