Search in sources :

Example 16 with RESTPermitUnimplemented

use of fi.otavanopisto.muikku.rest.RESTPermitUnimplemented in project muikku by otavanopisto.

the class WorkspaceRESTService method createWorkspaceMaterialReply.

@POST
// @Path ("/workspaces/{WORKSPACEENTITYID:[0-9]*}/materials/{WORKSPACEMATERIALID:[0-9]*}/replies")
@Path("/workspaces/{WORKSPACEENTITYID}/materials/{WORKSPACEMATERIALID}/replies")
@RESTPermitUnimplemented
public Response createWorkspaceMaterialReply(@PathParam("WORKSPACEENTITYID") Long workspaceEntityId, @PathParam("WORKSPACEMATERIALID") Long workspaceMaterialId, WorkspaceMaterialReply payload) {
    if (payload == null) {
        return Response.status(Status.BAD_REQUEST).entity("Payload is missing").build();
    }
    if (payload.getState() == null) {
        return Response.status(Status.BAD_REQUEST).entity("State is missing").build();
    }
    UserEntity loggedUser = sessionController.getLoggedUserEntity();
    if (loggedUser == null) {
        return Response.status(Status.UNAUTHORIZED).entity("Unauthorized").build();
    }
    WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceEntityId);
    if (workspaceEntity == null) {
        return Response.status(Status.NOT_FOUND).entity("Could not find workspace entity").build();
    }
    WorkspaceMaterial workspaceMaterial = workspaceMaterialController.findWorkspaceMaterialById(workspaceMaterialId);
    if (workspaceMaterial == null) {
        return Response.status(Status.NOT_FOUND).entity("Could not find workspace material").build();
    }
    WorkspaceRootFolder workspaceRootFolder = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceNode(workspaceMaterial);
    if (workspaceRootFolder == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity("Could not find workspace root folder").build();
    }
    if (!workspaceRootFolder.getWorkspaceEntityId().equals(workspaceEntity.getId())) {
        return Response.status(Status.BAD_REQUEST).entity("Invalid workspace material id or workspace entity id").build();
    }
    fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialReply workspaceMaterialReply = workspaceMaterialReplyController.createWorkspaceMaterialReply(workspaceMaterial, payload.getState(), loggedUser);
    return Response.ok(createRestModel(workspaceMaterialReply)).build();
}
Also used : WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) WorkspaceRootFolder(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceRootFolder) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)

Example 17 with RESTPermitUnimplemented

use of fi.otavanopisto.muikku.rest.RESTPermitUnimplemented in project muikku by otavanopisto.

the class WorkspaceRESTService method createWorkspaceFolder.

@POST
@Path("/workspaces/{WORKSPACEID}/folders/")
@RESTPermitUnimplemented
public Response createWorkspaceFolder(@PathParam("WORKSPACEID") Long workspaceEntityId, fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceFolder restFolder) {
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.UNAUTHORIZED).entity("Not logged in").build();
    }
    if (restFolder == null) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    // Workspace
    WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(workspaceEntityId);
    if (workspaceEntity == null) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    if (!sessionController.hasWorkspacePermission(MuikkuPermissions.MANAGE_WORKSPACE_MATERIALS, workspaceEntity)) {
        return Response.status(Status.FORBIDDEN).build();
    }
    WorkspaceNode rootFolder = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceEntity(workspaceEntity);
    WorkspaceNode nextSibling = restFolder.getNextSiblingId() == null ? null : workspaceMaterialController.findWorkspaceNodeById(restFolder.getNextSiblingId());
    WorkspaceFolder workspaceFolder = workspaceMaterialController.createWorkspaceFolder(rootFolder, "Untitled");
    if (nextSibling != null) {
        workspaceMaterialController.moveAbove(workspaceFolder, nextSibling);
    }
    return Response.ok(createRestModel(workspaceFolder)).build();
}
Also used : WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) WorkspaceNode(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode) WorkspaceFolder(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceFolder) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)

Example 18 with RESTPermitUnimplemented

use of fi.otavanopisto.muikku.rest.RESTPermitUnimplemented in project muikku by otavanopisto.

the class WebSocketRESTService method check.

@GET
@Path("/ticket/{TICKET}/check")
@RESTPermitUnimplemented
public Response check(@PathParam("TICKET") String ticketStr) {
    WebSocketTicket ticket = webSocketTicketController.findTicket(ticketStr);
    if (ticket != null) {
        UserEntity user = sessionController.getLoggedUserEntity();
        Long userId = user != null ? user.getId() : null;
        boolean valid = userId != null ? userId.equals(ticket.getUser()) : ticket.getUser() == null;
        if (valid)
            return Response.noContent().build();
        else
            return Response.status(Response.Status.NOT_FOUND).build();
    } else
        return Response.status(Response.Status.NOT_FOUND).build();
}
Also used : WebSocketTicket(fi.otavanopisto.muikku.plugins.websocket.WebSocketTicket) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) Path(javax.ws.rs.Path) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented) GET(javax.ws.rs.GET)

Example 19 with RESTPermitUnimplemented

use of fi.otavanopisto.muikku.rest.RESTPermitUnimplemented in project muikku by otavanopisto.

the class CoursePickerRESTService method listWorkspaces.

@GET
@Path("/workspaces/")
@RESTPermitUnimplemented
public Response listWorkspaces(@QueryParam("search") String searchString, @QueryParam("subjects") List<String> subjects, @QueryParam("educationTypes") List<String> educationTypeIds, @QueryParam("curriculums") List<String> curriculumIds, @QueryParam("minVisits") Long minVisits, @QueryParam("includeUnpublished") @DefaultValue("false") Boolean includeUnpublished, @QueryParam("myWorkspaces") @DefaultValue("false") Boolean myWorkspaces, @QueryParam("orderBy") List<String> orderBy, @QueryParam("firstResult") @DefaultValue("0") Integer firstResult, @QueryParam("maxResults") @DefaultValue("50") Integer maxResults, @Context Request request) {
    List<CoursePickerWorkspace> workspaces = new ArrayList<>();
    boolean doMinVisitFilter = minVisits != null;
    UserEntity userEntity = myWorkspaces ? sessionController.getLoggedUserEntity() : null;
    List<WorkspaceEntity> workspaceEntities = null;
    String schoolDataSourceFilter = null;
    List<String> workspaceIdentifierFilters = null;
    if (doMinVisitFilter) {
        if (userEntity != null) {
            workspaceEntities = workspaceVisitController.listWorkspaceEntitiesByMinVisitsOrderByLastVisit(userEntity, minVisits);
        } else {
            workspaceEntities = workspaceVisitController.listWorkspaceEntitiesByMinVisitsOrderByLastVisit(sessionController.getLoggedUserEntity(), minVisits);
        }
    } else {
        if (userEntity != null) {
            workspaceEntities = workspaceUserEntityController.listActiveWorkspaceEntitiesByUserEntity(userEntity);
        }
    }
    Iterator<SearchProvider> searchProviderIterator = searchProviders.iterator();
    if (searchProviderIterator.hasNext()) {
        SearchProvider searchProvider = searchProviderIterator.next();
        SearchResult searchResult = null;
        if (workspaceEntities != null) {
            workspaceIdentifierFilters = new ArrayList<>();
            for (WorkspaceEntity workspaceEntity : workspaceEntities) {
                if (schoolDataSourceFilter == null) {
                    schoolDataSourceFilter = workspaceEntity.getDataSource().getIdentifier();
                }
                workspaceIdentifierFilters.add(workspaceEntity.getIdentifier());
            }
        }
        List<WorkspaceAccess> accesses = new ArrayList<>(Arrays.asList(WorkspaceAccess.ANYONE));
        if (sessionController.isLoggedIn()) {
            accesses.add(WorkspaceAccess.LOGGED_IN);
            accesses.add(WorkspaceAccess.MEMBERS_ONLY);
        }
        List<Sort> sorts = null;
        if (orderBy != null && orderBy.contains("alphabet")) {
            sorts = new ArrayList<>();
            sorts.add(new Sort("name.untouched", Sort.Order.ASC));
        }
        List<SchoolDataIdentifier> educationTypes = null;
        if (educationTypeIds != null) {
            educationTypes = new ArrayList<>(educationTypeIds.size());
            for (String educationTypeId : educationTypeIds) {
                SchoolDataIdentifier educationTypeIdentifier = SchoolDataIdentifier.fromId(educationTypeId);
                if (educationTypeIdentifier != null) {
                    educationTypes.add(educationTypeIdentifier);
                } else {
                    return Response.status(Status.BAD_REQUEST).entity(String.format("Malformed education type identifier", educationTypeId)).build();
                }
            }
        }
        List<SchoolDataIdentifier> curriculumIdentifiers = null;
        if (curriculumIds != null) {
            curriculumIdentifiers = new ArrayList<>(curriculumIds.size());
            for (String curriculumId : curriculumIds) {
                SchoolDataIdentifier curriculumIdentifier = SchoolDataIdentifier.fromId(curriculumId);
                if (curriculumIdentifier != null) {
                    curriculumIdentifiers.add(curriculumIdentifier);
                } else {
                    return Response.status(Status.BAD_REQUEST).entity(String.format("Malformed curriculum identifier", curriculumId)).build();
                }
            }
        }
        searchResult = searchProvider.searchWorkspaces(schoolDataSourceFilter, subjects, workspaceIdentifierFilters, educationTypes, curriculumIdentifiers, searchString, accesses, sessionController.getLoggedUser(), includeUnpublished, firstResult, maxResults, sorts);
        schoolDataBridgeSessionController.startSystemSession();
        try {
            List<Map<String, Object>> results = searchResult.getResults();
            for (Map<String, Object> result : results) {
                String searchId = (String) result.get("id");
                if (StringUtils.isNotBlank(searchId)) {
                    String[] id = searchId.split("/", 2);
                    if (id.length == 2) {
                        String dataSource = id[1];
                        String identifier = id[0];
                        SchoolDataIdentifier workspaceIdentifier = new SchoolDataIdentifier(identifier, dataSource);
                        WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceByDataSourceAndIdentifier(workspaceIdentifier.getDataSource(), workspaceIdentifier.getIdentifier());
                        if (workspaceEntity != null) {
                            String name = (String) result.get("name");
                            String nameExtension = (String) result.get("nameExtension");
                            String description = (String) result.get("description");
                            boolean canSignup = getCanSignup(workspaceEntity);
                            boolean isCourseMember = getIsAlreadyOnWorkspace(workspaceEntity);
                            String educationTypeId = (String) result.get("educationTypeIdentifier");
                            String educationTypeName = null;
                            if (StringUtils.isNotBlank(educationTypeId)) {
                                EducationType educationType = null;
                                SchoolDataIdentifier educationTypeIdentifier = SchoolDataIdentifier.fromId(educationTypeId);
                                if (educationTypeIdentifier == null) {
                                    logger.severe(String.format("Malformatted educationTypeIdentifier %s", educationTypeId));
                                } else {
                                    educationType = courseMetaController.findEducationType(educationTypeIdentifier.getDataSource(), educationTypeIdentifier.getIdentifier());
                                }
                                if (educationType != null) {
                                    educationTypeName = educationType.getName();
                                }
                            }
                            if (StringUtils.isNotBlank(name)) {
                                workspaces.add(createRestModel(workspaceEntity, name, nameExtension, description, educationTypeName, canSignup, isCourseMember));
                            } else {
                                logger.severe(String.format("Search index contains workspace %s that does not have a name", workspaceIdentifier));
                            }
                        } else {
                            logger.severe(String.format("Search index contains workspace %s that does not exits on the school data system", workspaceIdentifier));
                        }
                    }
                }
            }
        } finally {
            schoolDataBridgeSessionController.endSystemSession();
        }
    } else {
        return Response.status(Status.INTERNAL_SERVER_ERROR).build();
    }
    if (workspaces.isEmpty()) {
        return Response.noContent().build();
    }
    if (orderBy.contains("lastVisit")) {
        Collections.sort(workspaces, new Comparator<CoursePickerWorkspace>() {

            @Override
            public int compare(CoursePickerWorkspace workspace1, CoursePickerWorkspace workspace2) {
                if (workspace1.getLastVisit() == null || workspace2.getLastVisit() == null) {
                    return 0;
                }
                if (workspace1.getLastVisit().before(workspace2.getLastVisit())) {
                    return 1;
                }
                if (workspace1.getLastVisit().after(workspace2.getLastVisit())) {
                    return -1;
                }
                return 0;
            }
        });
    }
    return Response.ok(workspaces).build();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) EducationType(fi.otavanopisto.muikku.schooldata.entity.EducationType) ArrayList(java.util.ArrayList) Sort(fi.otavanopisto.muikku.search.SearchProvider.Sort) SearchProvider(fi.otavanopisto.muikku.search.SearchProvider) SearchResult(fi.otavanopisto.muikku.search.SearchResult) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceAccess(fi.otavanopisto.muikku.model.workspace.WorkspaceAccess) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) Map(java.util.Map) Path(javax.ws.rs.Path) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented) GET(javax.ws.rs.GET)

Example 20 with RESTPermitUnimplemented

use of fi.otavanopisto.muikku.rest.RESTPermitUnimplemented in project muikku by otavanopisto.

the class ForgotPasswordRESTService method resetPassword.

@Path("/reset")
@GET
@RESTPermitUnimplemented
public Response resetPassword(@QueryParam("email") String email) {
    UserEntity userEntity = userEntityController.findUserEntityByEmailAddress(email);
    if (userEntity == null)
        return Response.status(Status.NOT_FOUND).build();
    try {
        UserPendingPasswordChange passwordChange = userPendingPasswordChangeDAO.findByUserEntity(userEntity);
        schoolDataBridgeSessionController.startSystemSession();
        try {
            String confirmationHash = userSchoolDataController.requestPasswordResetByEmail(userEntity.getDefaultSchoolDataSource(), email);
            if (passwordChange != null)
                passwordChange = userPendingPasswordChangeDAO.updateHash(passwordChange, confirmationHash);
            else
                passwordChange = userPendingPasswordChangeDAO.create(userEntity, confirmationHash);
            // TODO Email could be added to the reset link for added security (email+hash rather than just hash)
            String resetLink = baseUrl + "/forgotpassword/reset?h=" + passwordChange.getConfirmationHash();
            String mailSubject = localeController.getText(sessionController.getLocale(), "plugin.forgotpassword.mailSubject");
            String mailContent = localeController.getText(sessionController.getLocale(), "plugin.forgotpassword.mailContent", new String[] { resetLink });
            // TODO System sender address needs to be configurable
            mailer.sendMail(systemSettingsController.getSystemEmailSenderAddress(), email, mailSubject, mailContent);
        } finally {
            schoolDataBridgeSessionController.endSystemSession();
        }
        return Response.noContent().build();
    } catch (SchoolDataBridgeUnauthorizedException e) {
        return Response.status(Status.FORBIDDEN).build();
    }
}
Also used : UserPendingPasswordChange(fi.otavanopisto.muikku.plugins.user.UserPendingPasswordChange) SchoolDataBridgeUnauthorizedException(fi.otavanopisto.muikku.schooldata.SchoolDataBridgeUnauthorizedException) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) Path(javax.ws.rs.Path) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented) GET(javax.ws.rs.GET)

Aggregations

RESTPermitUnimplemented (fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)40 Path (javax.ws.rs.Path)40 GET (javax.ws.rs.GET)23 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)20 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)13 WorkspaceUserEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)10 POST (javax.ws.rs.POST)8 Material (fi.otavanopisto.muikku.plugins.material.model.Material)7 WorkspaceMaterial (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)7 ArrayList (java.util.ArrayList)7 PUT (javax.ws.rs.PUT)6 WorkspaceNode (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode)5 WorkspaceRootFolder (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceRootFolder)5 SchoolDataIdentifier (fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier)5 Produces (javax.ws.rs.Produces)5 HtmlMaterial (fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)4 MaterialMetaKey (fi.otavanopisto.muikku.plugins.material.model.MaterialMetaKey)4 User (fi.otavanopisto.muikku.schooldata.entity.User)4 CacheControl (javax.ws.rs.core.CacheControl)4 EntityTag (javax.ws.rs.core.EntityTag)4