Skip to content
This repository has been archived by the owner on Jun 9, 2021. It is now read-only.

Commit

Permalink
Speeding up restriction filter #237
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Aug 2, 2017
1 parent 0a74c8e commit aa4af99
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 150 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
Changelog of Pull Request Notifier for Bitbucket.

## Unreleased
### GitHub [#237](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/237) Listing all notifications via API takes a long time
Speeding up restriction filter

[e5b6bd9882559e8](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/e5b6bd9882559e8) Tomas Bjerre *2017-08-02 18:06:39*

### No issue
doc

[bacac00ee293995](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/bacac00ee293995) Tomas Bjerre *2017-07-31 06:47:15*
[0a74c8e65fa132e](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/0a74c8e65fa132e) Tomas Bjerre *2017-07-31 06:47:29*

## 3.9
### No issue
Expand Down
77 changes: 42 additions & 35 deletions src/main/java/se/bjurr/prnfb/presentation/ButtonServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

import com.atlassian.annotations.security.XsrfProtectionExcluded;
import com.google.common.base.Optional;
import com.google.common.collect.Iterables;

import se.bjurr.prnfb.http.NotificationResponse;
import se.bjurr.prnfb.presentation.dto.ButtonDTO;
import se.bjurr.prnfb.presentation.dto.ButtonFormElementDTO;
Expand All @@ -41,6 +37,11 @@
import se.bjurr.prnfb.service.SettingsService;
import se.bjurr.prnfb.service.UserCheckService;
import se.bjurr.prnfb.settings.PrnfbButton;
import se.bjurr.prnfb.settings.USER_LEVEL;

import com.atlassian.annotations.security.XsrfProtectionExcluded;
import com.google.common.base.Optional;
import com.google.common.collect.Iterables;

@Path("/settings/buttons")
public class ButtonServlet {
Expand All @@ -63,14 +64,16 @@ public ButtonServlet(
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
public Response create(ButtonDTO buttonDto) {
if (!userCheckService.isAdminAllowed(buttonDto)) {
final USER_LEVEL adminRestriction =
settingsService.getPrnfbSettingsData().getAdminRestriction();
if (!userCheckService.isAdminAllowed(buttonDto, adminRestriction)) {
return status(UNAUTHORIZED) //
.build();
}

PrnfbButton prnfbButton = toPrnfbButton(buttonDto);
PrnfbButton created = settingsService.addOrUpdateButton(prnfbButton);
ButtonDTO createdDto = toButtonDto(created);
final PrnfbButton prnfbButton = toPrnfbButton(buttonDto);
final PrnfbButton created = settingsService.addOrUpdateButton(prnfbButton);
final ButtonDTO createdDto = toButtonDto(created);

return status(OK) //
.entity(createdDto) //
Expand All @@ -82,8 +85,10 @@ public Response create(ButtonDTO buttonDto) {
@XsrfProtectionExcluded
@Produces(APPLICATION_JSON)
public Response delete(@PathParam("uuid") UUID prnfbButtonUuid) {
PrnfbButton prnfbButton = settingsService.getButton(prnfbButtonUuid);
if (!userCheckService.isAdminAllowed(prnfbButton)) {
final PrnfbButton prnfbButton = settingsService.getButton(prnfbButtonUuid);
final USER_LEVEL adminRestriction =
settingsService.getPrnfbSettingsData().getAdminRestriction();
if (!userCheckService.isAdminAllowed(prnfbButton, adminRestriction)) {
return status(UNAUTHORIZED) //
.build();
}
Expand All @@ -94,9 +99,9 @@ public Response delete(@PathParam("uuid") UUID prnfbButtonUuid) {
@GET
@Produces(APPLICATION_JSON)
public Response get() {
List<PrnfbButton> buttons = settingsService.getButtons();
Iterable<PrnfbButton> allowedButtons = userCheckService.filterAdminAllowed(buttons);
List<ButtonDTO> dtos = toButtonDtoList(allowedButtons);
final List<PrnfbButton> buttons = settingsService.getButtons();
final Iterable<PrnfbButton> allowedButtons = userCheckService.filterAdminAllowed(buttons);
final List<ButtonDTO> dtos = toButtonDtoList(allowedButtons);
Collections.sort(dtos);
return ok(dtos, APPLICATION_JSON).build();
}
Expand All @@ -105,9 +110,9 @@ public Response get() {
@Path("/projectKey/{projectKey}")
@Produces(APPLICATION_JSON)
public Response get(@PathParam("projectKey") String projectKey) {
List<PrnfbButton> buttons = settingsService.getButtons(projectKey);
Iterable<PrnfbButton> allowedButtons = userCheckService.filterAdminAllowed(buttons);
List<ButtonDTO> dtos = toButtonDtoList(allowedButtons);
final List<PrnfbButton> buttons = settingsService.getButtons(projectKey);
final Iterable<PrnfbButton> allowedButtons = userCheckService.filterAdminAllowed(buttons);
final List<ButtonDTO> dtos = toButtonDtoList(allowedButtons);
Collections.sort(dtos);
return ok(dtos, APPLICATION_JSON).build();
}
Expand All @@ -118,9 +123,9 @@ public Response get(@PathParam("projectKey") String projectKey) {
public Response get(
@PathParam("projectKey") String projectKey,
@PathParam("repositorySlug") String repositorySlug) {
List<PrnfbButton> buttons = settingsService.getButtons(projectKey, repositorySlug);
Iterable<PrnfbButton> allowedButtons = userCheckService.filterAdminAllowed(buttons);
List<ButtonDTO> dtos = toButtonDtoList(allowedButtons);
final List<PrnfbButton> buttons = settingsService.getButtons(projectKey, repositorySlug);
final Iterable<PrnfbButton> allowedButtons = userCheckService.filterAdminAllowed(buttons);
final List<ButtonDTO> dtos = toButtonDtoList(allowedButtons);
Collections.sort(dtos);
return ok(dtos, APPLICATION_JSON).build();
}
Expand All @@ -129,11 +134,13 @@ public Response get(
@Path("{uuid}")
@Produces(APPLICATION_JSON)
public Response get(@PathParam("uuid") UUID uuid) {
PrnfbButton button = settingsService.getButton(uuid);
if (!userCheckService.isAdminAllowed(button)) {
final PrnfbButton button = settingsService.getButton(uuid);
final USER_LEVEL adminRestriction =
settingsService.getPrnfbSettingsData().getAdminRestriction();
if (!userCheckService.isAdminAllowed(button, adminRestriction)) {
return status(UNAUTHORIZED).build();
}
ButtonDTO dto = toButtonDto(button);
final ButtonDTO dto = toButtonDto(button);
return ok(dto, APPLICATION_JSON).build();
}

Expand All @@ -143,8 +150,8 @@ public Response get(@PathParam("uuid") UUID uuid) {
public Response get(
@PathParam("repositoryId") Integer repositoryId,
@PathParam("pullRequestId") Long pullRequestId) {
List<PrnfbButton> buttons = buttonsService.getButtons(repositoryId, pullRequestId);
List<ButtonDTO> dtos = toButtonDtoList(buttons);
final List<PrnfbButton> buttons = buttonsService.getButtons(repositoryId, pullRequestId);
final List<ButtonDTO> dtos = toButtonDtoList(buttons);
Collections.sort(dtos);

populateButtonFormDtoList(repositoryId, pullRequestId, dtos);
Expand All @@ -161,31 +168,31 @@ public Response press(
@PathParam("repositoryId") Integer repositoryId,
@PathParam("pullRequestId") Long pullRequestId,
@PathParam("uuid") final UUID buttionUuid) {
List<PrnfbButton> buttons = buttonsService.getButtons(repositoryId, pullRequestId);
Optional<PrnfbButton> button =
final List<PrnfbButton> buttons = buttonsService.getButtons(repositoryId, pullRequestId);
final Optional<PrnfbButton> button =
Iterables.tryFind(buttons, (b) -> b.getUuid().equals(buttionUuid));
if (!button.isPresent()) {
return status(NOT_FOUND).build();
}
String formData = request.getParameter("form");
List<NotificationResponse> results =
final String formData = request.getParameter("form");
final List<NotificationResponse> results =
buttonsService.handlePressed(repositoryId, pullRequestId, buttionUuid, formData);

ButtonPressDTO dto = toTriggerResultDto(button.get(), results);
final ButtonPressDTO dto = toTriggerResultDto(button.get(), results);
return ok(dto, APPLICATION_JSON).build();
}

private void populateButtonFormDtoList(
Integer repositoryId, Long pullRequestId, List<ButtonDTO> dtos) {
for (ButtonDTO dto : dtos) {
PrnfbRendererWrapper renderer =
for (final ButtonDTO dto : dtos) {
final PrnfbRendererWrapper renderer =
buttonsService.getRenderer(repositoryId, pullRequestId, dto.getUuid());
List<ButtonFormElementDTO> buttonFormDtoList = dto.getButtonFormList();
final List<ButtonFormElementDTO> buttonFormDtoList = dto.getButtonFormList();
if (buttonFormDtoList != null) {
for (ButtonFormElementDTO buttonFormElementDto : buttonFormDtoList) {
String defaultValue = buttonFormElementDto.getDefaultValue();
for (final ButtonFormElementDTO buttonFormElementDto : buttonFormDtoList) {
final String defaultValue = buttonFormElementDto.getDefaultValue();
if (!isNullOrEmpty(defaultValue)) {
String defaultValueRendered = renderer.render(defaultValue, ENCODE_FOR.NONE);
final String defaultValueRendered = renderer.render(defaultValue, ENCODE_FOR.NONE);
buttonFormElementDto.setDefaultValue(defaultValueRendered);
}
}
Expand Down
50 changes: 29 additions & 21 deletions src/main/java/se/bjurr/prnfb/presentation/NotificationServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import com.atlassian.annotations.security.XsrfProtectionExcluded;

import se.bjurr.prnfb.presentation.dto.NotificationDTO;
import se.bjurr.prnfb.service.SettingsService;
import se.bjurr.prnfb.service.UserCheckService;
import se.bjurr.prnfb.settings.PrnfbNotification;
import se.bjurr.prnfb.settings.USER_LEVEL;

import com.atlassian.annotations.security.XsrfProtectionExcluded;

@Path("/settings/notifications")
public class NotificationServlet {
Expand All @@ -45,17 +46,20 @@ public NotificationServlet(SettingsService settingsService, UserCheckService use
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
public Response create(NotificationDTO notificationDto) {
if (!this.userCheckService.isAdminAllowed(notificationDto)) {
final USER_LEVEL adminRestriction =
settingsService.getPrnfbSettingsData().getAdminRestriction();
if (!this.userCheckService.isAdminAllowed(notificationDto, adminRestriction)) {
return status(UNAUTHORIZED).build();
}
try {
PrnfbNotification prnfbNotification = toPrnfbNotification(notificationDto);
PrnfbNotification created = this.settingsService.addOrUpdateNotification(prnfbNotification);
NotificationDTO createdDto = toNotificationDto(created);
final PrnfbNotification prnfbNotification = toPrnfbNotification(notificationDto);
final PrnfbNotification created =
this.settingsService.addOrUpdateNotification(prnfbNotification);
final NotificationDTO createdDto = toNotificationDto(created);
return status(OK) //
.entity(createdDto) //
.build();
} catch (Exception e) {
} catch (final Exception e) {
throw propagate(e);
}
}
Expand All @@ -65,8 +69,10 @@ public Response create(NotificationDTO notificationDto) {
@XsrfProtectionExcluded
@Produces(APPLICATION_JSON)
public Response delete(@PathParam("uuid") UUID notification) {
PrnfbNotification notificationDto = this.settingsService.getNotification(notification);
if (!this.userCheckService.isAdminAllowed(notificationDto)) {
final PrnfbNotification notificationDto = this.settingsService.getNotification(notification);
final USER_LEVEL adminRestriction =
settingsService.getPrnfbSettingsData().getAdminRestriction();
if (!this.userCheckService.isAdminAllowed(notificationDto, adminRestriction)) {
return status(UNAUTHORIZED).build();
}
this.settingsService.deleteNotification(notification);
Expand All @@ -76,10 +82,10 @@ public Response delete(@PathParam("uuid") UUID notification) {
@GET
@Produces(APPLICATION_JSON)
public Response get() {
List<PrnfbNotification> notifications = this.settingsService.getNotifications();
Iterable<PrnfbNotification> notificationsFiltered =
final List<PrnfbNotification> notifications = this.settingsService.getNotifications();
final Iterable<PrnfbNotification> notificationsFiltered =
userCheckService.filterAdminAllowed(notifications);
List<NotificationDTO> dtos = toNotificationDtoList(notificationsFiltered);
final List<NotificationDTO> dtos = toNotificationDtoList(notificationsFiltered);
Collections.sort(dtos);
return ok(dtos).build();
}
Expand All @@ -88,10 +94,10 @@ public Response get() {
@Path("/projectKey/{projectKey}")
@Produces(APPLICATION_JSON)
public Response get(@PathParam("projectKey") String projectKey) {
List<PrnfbNotification> notifications = this.settingsService.getNotifications(projectKey);
Iterable<PrnfbNotification> notificationsFiltered =
final List<PrnfbNotification> notifications = this.settingsService.getNotifications(projectKey);
final Iterable<PrnfbNotification> notificationsFiltered =
userCheckService.filterAdminAllowed(notifications);
List<NotificationDTO> dtos = toNotificationDtoList(notificationsFiltered);
final List<NotificationDTO> dtos = toNotificationDtoList(notificationsFiltered);
Collections.sort(dtos);
return ok(dtos).build();
}
Expand All @@ -102,11 +108,11 @@ public Response get(@PathParam("projectKey") String projectKey) {
public Response get(
@PathParam("projectKey") String projectKey,
@PathParam("repositorySlug") String repositorySlug) {
List<PrnfbNotification> notifications =
final List<PrnfbNotification> notifications =
this.settingsService.getNotifications(projectKey, repositorySlug);
Iterable<PrnfbNotification> notificationsFiltered =
final Iterable<PrnfbNotification> notificationsFiltered =
userCheckService.filterAdminAllowed(notifications);
List<NotificationDTO> dtos = toNotificationDtoList(notificationsFiltered);
final List<NotificationDTO> dtos = toNotificationDtoList(notificationsFiltered);
Collections.sort(dtos);
return ok(dtos).build();
}
Expand All @@ -115,11 +121,13 @@ public Response get(
@Path("{uuid}")
@Produces(APPLICATION_JSON)
public Response get(@PathParam("uuid") UUID notificationUuid) {
PrnfbNotification notification = this.settingsService.getNotification(notificationUuid);
if (!this.userCheckService.isAdminAllowed(notification)) {
final PrnfbNotification notification = this.settingsService.getNotification(notificationUuid);
final USER_LEVEL adminRestriction =
settingsService.getPrnfbSettingsData().getAdminRestriction();
if (!this.userCheckService.isAdminAllowed(notification, adminRestriction)) {
return status(UNAUTHORIZED).build();
}
NotificationDTO dto = toNotificationDto(notification);
final NotificationDTO dto = toNotificationDto(notification);
return ok(dto).build();
}
}
18 changes: 11 additions & 7 deletions src/main/java/se/bjurr/prnfb/presentation/SettingsDataServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import com.atlassian.annotations.security.XsrfProtectionExcluded;
import com.google.common.base.Optional;

import se.bjurr.prnfb.presentation.dto.SettingsDataDTO;
import se.bjurr.prnfb.service.SettingsService;
import se.bjurr.prnfb.service.UserCheckService;
import se.bjurr.prnfb.settings.PrnfbSettingsData;
import se.bjurr.prnfb.settings.Restricted;
import se.bjurr.prnfb.settings.USER_LEVEL;

import com.atlassian.annotations.security.XsrfProtectionExcluded;
import com.google.common.base.Optional;

@Path("/settings")
public class SettingsDataServlet {
Expand All @@ -41,8 +42,8 @@ public Response get() {
return status(UNAUTHORIZED).build();
}

PrnfbSettingsData settingsData = this.settingsService.getPrnfbSettingsData();
SettingsDataDTO settingsDataDto = toDto(settingsData);
final PrnfbSettingsData settingsData = this.settingsService.getPrnfbSettingsData();
final SettingsDataDTO settingsDataDto = toDto(settingsData);

return ok(settingsDataDto).build();
}
Expand All @@ -52,6 +53,8 @@ public Response get() {
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
public Response post(SettingsDataDTO settingsDataDto) {
final USER_LEVEL adminRestriction =
settingsService.getPrnfbSettingsData().getAdminRestriction();
if (!this.userCheckService.isAdminAllowed(
new Restricted() {
@Override
Expand All @@ -63,11 +66,12 @@ public Optional<String> getRepositorySlug() {
public Optional<String> getProjectKey() {
return Optional.absent();
}
})) {
},
adminRestriction)) {
return status(UNAUTHORIZED).build();
}

PrnfbSettingsData prnfbSettingsData = toPrnfbSettingsData(settingsDataDto);
final PrnfbSettingsData prnfbSettingsData = toPrnfbSettingsData(settingsDataDto);
this.settingsService.setPrnfbSettingsData(prnfbSettingsData);

return noContent().build();
Expand Down
Loading

0 comments on commit aa4af99

Please sign in to comment.