PublishSearchPromoteConfigHandler.java
6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.felix.scr.annotations.Activate
* 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.sling.api.resource.LoginException
* org.apache.sling.api.resource.ModifiableValueMap
* org.apache.sling.api.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.commons.osgi.PropertiesUtil
* org.osgi.service.component.ComponentContext
* org.osgi.service.component.ComponentException
* org.osgi.service.event.Event
* org.osgi.service.event.EventHandler
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.searchpromote.impl;
import com.day.cq.searchpromote.SearchPromoteException;
import com.day.cq.searchpromote.SearchPromoteService;
import com.day.cq.searchpromote.impl.SearchPromoteEnvironment;
import java.util.Dictionary;
import java.util.Map;
import org.apache.felix.scr.annotations.Activate;
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.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.ComponentException;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Service(value={EventHandler.class})
@Component(metatype=1, label="AEM 6 - Search&Promote integration publish configuration handler", description="Handler that updates the S&P cloud configuration when this configuration is pushed to publish")
@Properties(value={@Property(name="event.topics", value={"org/apache/sling/api/resource/Resource/ADDED", "org/apache/sling/api/resource/Resource/CHANGED"}, propertyPrivate=1), @Property(name="event.filter", value={"(&(path=/etc/cloudservices/search-promote/*/jcr:content)(!(event.application=*)))"}, propertyPrivate=1)})
public class PublishSearchPromoteConfigHandler
implements EventHandler {
private final Logger log;
private static final boolean DEFAULT_ENABLED = false;
@Property(boolValue={0}, label="Enabled", description="Enables the configuration handler. It must be disabled on author instances.")
private static final String PROPERTY_ENABLED = "cq.searchpromote.confighandler.enabled";
@Reference
private SearchPromoteService searchPromoteService;
@Reference
private ResourceResolverFactory resolverFactory;
private boolean enabled;
public PublishSearchPromoteConfigHandler() {
this.log = LoggerFactory.getLogger(this.getClass());
}
@Activate
public void activate(ComponentContext componentContext) throws ComponentException {
this.log.debug("Activating Search&Promote publish configuration handler...");
this.enabled = PropertiesUtil.toBoolean(componentContext.getProperties().get("cq.searchpromote.confighandler.enabled"), (boolean)false);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void handleEvent(Event event) {
block10 : {
if (!this.enabled) {
return;
}
String resourcePath = (String)event.getProperty("path");
this.log.debug("Detected {} at {}", (Object)event.getTopic(), (Object)resourcePath);
ResourceResolver resolver = null;
try {
resolver = this.getServiceResolver();
Resource resource = resolver.getResource(resourcePath);
if (resource != null) {
ModifiableValueMap props = (ModifiableValueMap)resource.adaptTo(ModifiableValueMap.class);
try {
this.log.debug("Retrieving search form from LIVE environment...");
String formXml = this.searchPromoteService.getSearchForm((String)props.get("memberid", (Object)""), (String)props.get("accountno", (Object)""), SearchPromoteEnvironment.LIVE);
props.put((Object)"searchformxml", (Object)formXml);
this.log.debug("Form retrieved. Persisting changes...");
resolver.commit();
this.log.debug("Changes saved in {}", (Object)resourcePath);
}
catch (SearchPromoteException e) {
this.log.error(e.getMessage(), (Throwable)e);
}
catch (PersistenceException e) {
this.log.error(e.getMessage(), (Throwable)e);
}
break block10;
}
this.log.error("Searchpromote Service User could not access resource {}", (Object)resourcePath);
}
catch (LoginException ex) {
this.log.error(ex.getMessage(), (Throwable)ex);
}
finally {
this.closeResolver(resolver);
}
}
}
private ResourceResolver getServiceResolver() throws LoginException {
return this.resolverFactory.getServiceResourceResolver(null);
}
private void closeResolver(ResourceResolver resolver) {
if (resolver != null) {
resolver.close();
}
}
protected void bindSearchPromoteService(SearchPromoteService searchPromoteService) {
this.searchPromoteService = searchPromoteService;
}
protected void unbindSearchPromoteService(SearchPromoteService searchPromoteService) {
if (this.searchPromoteService == searchPromoteService) {
this.searchPromoteService = null;
}
}
protected void bindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resolverFactory = resourceResolverFactory;
}
protected void unbindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resolverFactory == resourceResolverFactory) {
this.resolverFactory = null;
}
}
}