Search in sources :

Example 1 with ISearch

use of com.willshex.blogwt.server.service.search.ISearch in project blogwt by billy1380.

the class SearchAllActionHandler method handle.

/* (non-Javadoc)
	 * 
	 * @see
	 * com.willshex.gson.web.service.server.ActionHandler#handle(com.willshex.
	 * gson.web.service.shared.Request,
	 * com.willshex.gson.web.service.shared.Response) */
@SuppressWarnings("unchecked")
@Override
protected void handle(SearchAllRequest input, SearchAllResponse output) throws Exception {
    ApiValidator.request(input, SearchAllRequest.class);
    ApiValidator.accessCode(input.accessCode, "input.accessCode");
    try {
        output.session = input.session = SessionValidator.lookupCheckAndExtend(input.session, "input.session");
    } catch (InputValidationException ex) {
        output.session = input.session = null;
    }
    if (input.query == null)
        ApiValidator.throwServiceError(InputValidationException.class, ApiError.InvalidValueNull, "String: input.query");
    output.posts = SearchHelper.pagedAndSorted((ISearch<Post>) PostServiceProvider.provide(), input.query, Integer.valueOf(0), SearchHelper.SHORT_SEARCH_LIMIT, null, null);
    Map<Key<User>, User> users = new HashMap<Key<User>, User>();
    if (output.posts != null) {
        for (Post post : output.posts) {
            if (users.get(post.authorKey) == null) {
                users.put(post.authorKey, UserServiceProvider.provide().getUser(keyToId(post.authorKey)));
            }
            post.author = users.get(post.authorKey);
        }
    }
    output.pages = SearchHelper.pagedAndSorted((ISearch<Page>) PageServiceProvider.provide(), input.query, Integer.valueOf(0), SearchHelper.SHORT_SEARCH_LIMIT, null, null);
    if (output.pages != null) {
        for (Page page : output.pages) {
            if (users.get(page.ownerKey) == null) {
                users.put(page.ownerKey, UserServiceProvider.provide().getUser(keyToId(page.ownerKey)));
            }
            page.owner = users.get(page.ownerKey);
        }
    }
    output.users = SearchHelper.pagedAndSorted((ISearch<User>) UserServiceProvider.provide(), input.query, Integer.valueOf(0), SearchHelper.SHORT_SEARCH_LIMIT, null, null);
}
Also used : User(com.willshex.blogwt.shared.api.datatype.User) HashMap(java.util.HashMap) Post(com.willshex.blogwt.shared.api.datatype.Post) ISearch(com.willshex.blogwt.server.service.search.ISearch) InputValidationException(com.willshex.gson.web.service.server.InputValidationException) Page(com.willshex.blogwt.shared.api.datatype.Page) Key(com.googlecode.objectify.Key)

Example 2 with ISearch

use of com.willshex.blogwt.server.service.search.ISearch in project blogwt by billy1380.

the class DevServlet method doGet.

/* (non-Javadoc)
	 * 
	 * @see com.willshex.service.ContextAwareServlet#doGet() */
@Override
protected void doGet() throws ServletException, IOException {
    super.doGet();
    String action = REQUEST.get().getParameter("action");
    if (action != null) {
        action = action.toLowerCase();
    }
    if ("gentags".equals(action)) {
        TagServiceProvider.provide().generateTags();
    } else if (action != null && action.startsWith("index")) {
        String group = REQUEST.get().getParameter("group");
        if (group == null || group.trim().isEmpty()) {
            group = "blogwt";
        }
        PageServiceProvider.provide();
        PostServiceProvider.provide();
        UserServiceProvider.provide();
        SearchHelper.indexAll((ISearch<?>) ServiceDiscovery.getService(group + "." + action.replace("index", "")));
    } else if ("clearsearch".equals(action)) {
        PersistenceServiceProvider.provide();
        String name = REQUEST.get().getParameter("index");
        String ids = REQUEST.get().getParameter("ids");
        String namespace = REQUEST.get().getParameter("ns");
        String[] split = ids.split(",");
        for (String id : split) {
            SearchHelper.deleteSearch(() -> namespace == null ? false : Boolean.valueOf(namespace).booleanValue(), name, id);
        }
    } else if ("linkall".equals(action)) {
        PostServiceProvider.provide().linkAll();
    } else if ("clearlinks".equals(action)) {
        PostServiceProvider.provide().clearLinks();
    } else if ("archiveall".equals(action)) {
        ArchiveEntryServiceProvider.provide().generateArchive();
    } else if ("fixroles".equals(action)) {
        Collection<Role> all = RoleHelper.createAll();
        all.stream().forEach(role -> {
            Role loaded = RoleServiceProvider.provide().getCodeRole(role.code);
            if (loaded == null || loaded.id == null) {
                RoleServiceProvider.provide().addRole(role);
            }
            if (role.permissions != null) {
                role.permissions.stream().forEach(i -> {
                    Permission lp = PermissionServiceProvider.provide().getCodePermission(i.code);
                    if (lp == null) {
                        if (LOG.isLoggable(Level.WARNING)) {
                            LOG.warning("Could not find permission with code [" + i.code + "], might want to run [fixpermissions] action");
                        }
                    } else {
                        if (loaded.permissions == null) {
                            loaded.permissions = new ArrayList<>();
                        }
                        loaded.permissions.add(lp);
                    }
                });
                RoleServiceProvider.provide().updateRole(loaded);
            }
        });
    } else if ("fixpermissions".equals(action)) {
        Collection<Permission> all = PermissionHelper.createAll();
        Permission loaded;
        for (Permission permission : all) {
            loaded = PermissionServiceProvider.provide().getCodePermission(permission.code);
            if (loaded == null || loaded.id == null) {
                PermissionServiceProvider.provide().addPermission(permission);
            }
        }
    } else if ("getposts".equals(action)) {
        RESPONSE.get().getOutputStream().print(JsonUtils.beautifyJson((new GetPostsActionHandler()).handle((GetPostsRequest) new GetPostsRequest().showAll(Boolean.TRUE).pager(PagerHelper.createDefaultPager()).accessCode(ApiValidator.DEV_ACCESS_CODE)).toString()));
    } else if ("staticurl".equals(action)) {
        List<Resource> resources = ResourceServiceProvider.provide().getResources(Integer.valueOf(0), Integer.valueOf(Integer.MAX_VALUE), null, null);
        JsonObject object;
        for (Resource resource : resources) {
            if (resource.properties != null) {
                if (resource.properties.contains(":image")) {
                    resource.properties = resource.properties.replace(":image", ":\"image").replace("}", "\"}");
                }
                object = new JsonParser().parse(resource.properties).getAsJsonObject();
            } else {
                object = new JsonObject();
            }
            if (!object.has("staticUrl") || object.get("staticUrl").getAsString().startsWith("http")) {
                try {
                    object.addProperty("staticUrl", ImagesServiceFactory.getImagesService().getServingUrl(ServingUrlOptions.Builder.withBlobKey(new BlobKey(resource.data.replace("gs://", "")))).replaceFirst("https:\\/\\/", "//").replaceFirst("http:\\/\\/", "//"));
                } catch (Throwable e) {
                    if (LOG.isLoggable(Level.FINE)) {
                        LOG.fine("Could not update resource");
                    }
                }
                resource.properties = object.toString();
                ResourceServiceProvider.provide().updateResource(resource);
            }
        }
    } else if ("fixmetanotifications".equals(action)) {
        List<MetaNotification> metas = MetaNotificationHelper.createAll();
        for (MetaNotification meta : metas) {
            if (MetaNotificationServiceProvider.provide().getCodeMetaNotification(meta.code) == null) {
                meta = MetaNotificationServiceProvider.provide().addMetaNotification(meta);
                LOG.info("added meta notification [" + meta.code + "] with id [" + meta.id + "]");
            } else {
                LOG.info("Meta notification [" + meta.code + "] already exists");
            }
        }
    } else if ("admin".equals(action)) {
        User user = UserServiceProvider.provide().getUsernameUser(REQUEST.get().getParameter("user"));
        UserServiceProvider.provide().addUserRolesAndPermissions(user, Arrays.asList(RoleServiceProvider.provide().getCodeRole(RoleHelper.ADMIN)), null);
    } else if ("genandshowdownload".equals(action)) {
        String idParam = REQUEST.get().getParameter("id");
        Long id = Long.valueOf(idParam);
        GeneratedDownload d = GeneratedDownloadServiceProvider.provide().getGeneratedDownload(id);
        Stack stack = Stack.parse(d.parameters);
        Filter filter = Filter.fromStack(stack);
        switch(filter.type) {
            default:
                break;
        }
    } else if (action != null && action.startsWith("resave")) {
        String typeName = REQUEST.get().getParameter("type");
        ResaveServlet.queueForResaving(typeName);
    } else if (action != null && action.startsWith("deleteposts")) {
        processPaged((Integer s, Integer c, PostSortType o, SortDirectionType d) -> PostServiceProvider.provide().getPosts(Boolean.TRUE, Boolean.FALSE, s, c, o, d), PostServiceProvider.provide()::deletePost);
    }
}
Also used : GetPostsActionHandler(com.willshex.blogwt.server.api.blog.action.GetPostsActionHandler) User(com.willshex.blogwt.shared.api.datatype.User) GetPostsRequest(com.willshex.blogwt.shared.api.blog.call.GetPostsRequest) JsonObject(com.google.gson.JsonObject) PostSortType(com.willshex.blogwt.shared.api.datatype.PostSortType) BlobKey(com.google.appengine.api.blobstore.BlobKey) ISearch(com.willshex.blogwt.server.service.search.ISearch) Permission(com.willshex.blogwt.shared.api.datatype.Permission) List(java.util.List) ArrayList(java.util.ArrayList) JsonParser(com.google.gson.JsonParser) Resource(com.willshex.blogwt.shared.api.datatype.Resource) MetaNotification(com.willshex.blogwt.shared.api.datatype.MetaNotification) SortDirectionType(com.willshex.blogwt.shared.api.SortDirectionType) Stack(com.willshex.blogwt.shared.page.Stack) Role(com.willshex.blogwt.shared.api.datatype.Role) GeneratedDownload(com.willshex.blogwt.shared.api.datatype.GeneratedDownload) Filter(com.willshex.blogwt.shared.page.search.Filter) Collection(java.util.Collection)

Example 3 with ISearch

use of com.willshex.blogwt.server.service.search.ISearch in project blogwt by billy1380.

the class SearchIndexServlet method doGet.

/* (non-Javadoc)
	 * 
	 * @see com.willshex.service.ContextAwareServlet#doGet() */
@Override
protected void doGet() throws ServletException, IOException {
    super.doGet();
    String appEngineQueue = REQUEST.get().getHeader("X-AppEngine-QueueName");
    if (LOG.isLoggable(Level.FINE)) {
        LOG.log(Level.FINE, String.format("appEngineQueue is [%s]", appEngineQueue));
    }
    boolean isQueue = appEngineQueue != null && "default".toLowerCase().equals(appEngineQueue.toLowerCase());
    if (!isQueue) {
        RESPONSE.get().setStatus(401);
        RESPONSE.get().getOutputStream().print("failure");
        LOG.log(Level.WARNING, "Attempt to run script directly, this is not permitted");
        return;
    }
    if (LOG.isLoggable(Level.FINE)) {
        LOG.log(Level.FINE, String.format("Call from [%s] allowed to proceed", appEngineQueue));
    }
    String nameParam = REQUEST.get().getParameter(SearchHelper.ENTITY_NAME_KEY);
    String idParam = REQUEST.get().getParameter(SearchHelper.ENTITY_ID_KEY);
    if (nameParam != null && idParam != null) {
        Long id = Long.valueOf(idParam);
        IService service = ServiceDiscovery.getService(nameParam);
        if (service != null && service instanceof ISearch) {
            SearchHelper.index((ISearch<?>) service, id);
        }
    }
}
Also used : ISearch(com.willshex.blogwt.server.service.search.ISearch) IService(com.willshex.service.IService)

Aggregations

ISearch (com.willshex.blogwt.server.service.search.ISearch)3 User (com.willshex.blogwt.shared.api.datatype.User)2 BlobKey (com.google.appengine.api.blobstore.BlobKey)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 Key (com.googlecode.objectify.Key)1 GetPostsActionHandler (com.willshex.blogwt.server.api.blog.action.GetPostsActionHandler)1 SortDirectionType (com.willshex.blogwt.shared.api.SortDirectionType)1 GetPostsRequest (com.willshex.blogwt.shared.api.blog.call.GetPostsRequest)1 GeneratedDownload (com.willshex.blogwt.shared.api.datatype.GeneratedDownload)1 MetaNotification (com.willshex.blogwt.shared.api.datatype.MetaNotification)1 Page (com.willshex.blogwt.shared.api.datatype.Page)1 Permission (com.willshex.blogwt.shared.api.datatype.Permission)1 Post (com.willshex.blogwt.shared.api.datatype.Post)1 PostSortType (com.willshex.blogwt.shared.api.datatype.PostSortType)1 Resource (com.willshex.blogwt.shared.api.datatype.Resource)1 Role (com.willshex.blogwt.shared.api.datatype.Role)1 Stack (com.willshex.blogwt.shared.page.Stack)1 Filter (com.willshex.blogwt.shared.page.search.Filter)1 InputValidationException (com.willshex.gson.web.service.server.InputValidationException)1