Utility Methods
These helper methods are reusable across controllers and can be moved to a separate utility class.
Methods
Copied to your clipboard1public class TargetRequestUtils {23 public static Context getContext(HttpServletRequest request) {4 Context context = new Context()5 .channel(ChannelType.WEB)6 .timeOffsetInMinutes(330.0)7 .address(getAddress(request));8 return context;9 }1011 public static Address getAddress(HttpServletRequest request) {12 Address address = new Address()13 .referringUrl(request.getHeader("referer"))14 .url(request.getRequestURL().toString());15 return address;16 }1718 public static List<TargetCookie> getTargetCookies(Cookie[] cookies) {19 if (cookies == null) {20 return Collections.emptyList();21 }22 return Arrays.stream(cookies)23 .filter(Objects::nonNull)24 .filter(cookie -> CookieUtils.getTargetCookieNames().contains(cookie.getName()))25 .map(cookie -> new TargetCookie(cookie.getName(), cookie.getValue(), cookie.getMaxAge()))26 .collect(Collectors.toList());27 }2829 public static HttpServletResponse setCookies(List<TargetCookie> targetCookies,30 HttpServletResponse response) {31 targetCookies32 .stream()33 .map(targetCookie -> new Cookie(targetCookie.getName(), targetCookie.getValue()))34 .forEach(cookie -> {35 cookie.setPath("/");36 response.addCookie(cookie);37 });38 return response;39 }4041 public static List<MboxRequest> getMboxRequests(String... name) {42 List<MboxRequest> mboxRequests = new ArrayList<>();43 for (int i = 0; i < name.length; i++) {44 mboxRequests.add(new MboxRequest().name(name[i]).index(i));45 }46 return mboxRequests;47 }4849 public static PrefetchRequest getPrefetchRequest() {50 PrefetchRequest prefetchRequest = new PrefetchRequest();51 ViewRequest viewRequest = new ViewRequest();52 prefetchRequest.setViews(Arrays.asList(viewRequest));53 return prefetchRequest;54 }55}