Skip to content

Commit

Permalink
Update indexing handler
Browse files Browse the repository at this point in the history
  • Loading branch information
cnapagoda committed Jun 27, 2015
1 parent d0f416d commit 0719b9e
Showing 1 changed file with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.registry.core.*;
import org.wso2.carbon.registry.core.config.Mount;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.jdbc.handlers.Handler;
import org.wso2.carbon.registry.core.jdbc.handlers.RequestContext;
import org.wso2.carbon.registry.core.session.CurrentSession;
import org.wso2.carbon.registry.core.session.UserRegistry;
import org.wso2.carbon.registry.core.utils.MediaTypesUtils;
import org.wso2.carbon.registry.core.utils.RegistryUtils;
import org.wso2.carbon.registry.extensions.utils.CommonUtil;
import org.wso2.carbon.registry.indexing.AsyncIndexer.File2Index;
import org.wso2.carbon.registry.indexing.indexer.IndexDocumentCreator;
import org.wso2.carbon.registry.indexing.indexer.IndexerException;
Expand Down Expand Up @@ -66,17 +68,18 @@ public class IndexingHandler extends Handler {

public void put(RequestContext requestContext) throws RegistryException {
if (log.isDebugEnabled()){
log.debug(" Before put resources into indexer");
log.debug(" Before put resources into indexer " + requestContext.getResourcePath().getPath());
}
if (isIndexable(requestContext)) {
if (isIndexablePutOperation(requestContext) || Utils.getRegistryService() == null) {
return;
}
PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();

submitFileForIndexing(getIndexer(), requestContext.getResource(), requestContext.getResourcePath().getPath(),
String path = getRegistryPath(requestContext);
submitFileForIndexing(getIndexer(), requestContext.getResource(), path,
null,carbonContext.getTenantId(), carbonContext.getTenantDomain() );
if (log.isDebugEnabled()){
log.debug(" After put resources into indexer");
log.debug(" After put resources into indexer "+ requestContext.getResourcePath().getPath());
}
}

Expand All @@ -97,7 +100,9 @@ public String move(RequestContext requestContext) throws RegistryException {
}
Resource resource = requestContext.getRegistry().get(oldPath);
PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
submitFileForIndexing(getIndexer(), resource, newPath, null, carbonContext.getTenantId(), carbonContext.getTenantDomain());
String path = CommonUtil.getRegistryPath(requestContext.getRegistry().getRegistryContext(),newPath);
submitFileForIndexing(getIndexer(), resource, path, null, carbonContext.getTenantId(),
carbonContext.getTenantDomain());
return super.move(requestContext);
}

Expand All @@ -122,7 +127,9 @@ public String rename(RequestContext requestContext) throws RegistryException {
}
Resource resource = requestContext.getRegistry().get(oldPath);
PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
submitFileForIndexing(getIndexer(), resource, newPath, null, carbonContext.getTenantId(), carbonContext.getTenantDomain());
String path = CommonUtil.getRegistryPath(requestContext.getRegistry().getRegistryContext(),newPath);
submitFileForIndexing(getIndexer(), resource, path, null, carbonContext.getTenantId(),
carbonContext.getTenantDomain());
return super.rename(requestContext);
}

Expand All @@ -135,8 +142,10 @@ public String copy(RequestContext requestContext) throws RegistryException {
String newPath = requestContext.getTargetPath();
Resource resource = requestContext.getRegistry().get(oldPath);
PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
String path = CommonUtil.getRegistryPath(requestContext.getRegistry().getRegistryContext(),newPath);

submitFileForIndexing(getIndexer(), resource, newPath, null, carbonContext.getTenantId(), carbonContext.getTenantDomain());
submitFileForIndexing(getIndexer(), resource, path, null, carbonContext.getTenantId(),
carbonContext.getTenantDomain());
return super.copy(requestContext);
}

Expand All @@ -147,6 +156,13 @@ private boolean isIndexable(RequestContext requestContext) {
|| requestContext.getResource() instanceof Collection;
}

private boolean isIndexablePutOperation(RequestContext requestContext) {
return (requestContext.getRegistry().getRegistryContext() == null)
|| requestContext.getResource() == null
|| requestContext.getResource().getMediaType() == null
|| requestContext.getResource() instanceof Collection;
}

private boolean isExecutingMountedHandlerChain(RequestContext requestContext) {
return (requestContext.getRegistry().getRegistryContext() == null)
|| requestContext.getRegistry().getRegistryContext().isClone();
Expand Down Expand Up @@ -216,8 +232,9 @@ public void importResource(RequestContext requestContext) throws RegistryExcepti
}

PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();

submitFileForIndexing(getIndexer(), requestContext.getResource(), requestContext.getResourcePath().getPath(), requestContext.getSourceURL(), carbonContext.getTenantId(), carbonContext.getTenantDomain() );
String path = getRegistryPath(requestContext);
submitFileForIndexing(getIndexer(), requestContext.getResource(), path, requestContext.getSourceURL(),
carbonContext.getTenantId(), carbonContext.getTenantDomain());
}

public void delete(RequestContext requestContext) throws RegistryException {
Expand Down Expand Up @@ -261,7 +278,8 @@ private AsyncIndexer getIndexer() throws RegistryException {
}
}

private void submitFileForIndexing(AsyncIndexer indexer, Resource resource, String path, String sourceURL, int tenantId, String tenantDomain) {
private void submitFileForIndexing(AsyncIndexer indexer, Resource resource, String path, String sourceURL,
int tenantId, String tenantDomain) {
//if media type is null, mostly it is not a file. We will skip.
String mediaType = resource.getMediaType();
if (mediaType == null && path != null) {
Expand All @@ -271,8 +289,8 @@ private void submitFileForIndexing(AsyncIndexer indexer, Resource resource, Stri
// We are only making an attempt to determine the media type.
}
}
if (mediaType == null || IndexingManager.getInstance().getIndexerForMediaType(mediaType)
== null) {
if (mediaType == null || Utils.getRegistryService() == null ||
IndexingManager.getInstance().getIndexerForMediaType(mediaType) == null) {
return;
}
if (log.isDebugEnabled()) {
Expand Down Expand Up @@ -308,4 +326,17 @@ private void submitFileForIndexing(AsyncIndexer indexer, Resource resource, Stri
}
}

private String getRegistryPath(RequestContext requestContext) {
String servicePath = requestContext.getResourcePath().getPath();
List<Mount> mounts = requestContext.getRegistry().getRegistryContext().getMounts();
for (Mount mount: mounts) {
String mountPath = mount.getPath();
if (servicePath.startsWith(mount.getTargetPath())){
return servicePath.replace(mount.getTargetPath(), mountPath);
}
}
return servicePath;
}


}

0 comments on commit 0719b9e

Please sign in to comment.