Edit in GitHubLog an issue

Utility Methods

These helper methods are reusable across controllers and can be moved to a separate utility class.

Copied to your clipboard
1public class TargetRequestUtils {
2
3 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 }
10
11 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 }
17
18 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 }
28
29 public static HttpServletResponse setCookies(List<TargetCookie> targetCookies,
30 HttpServletResponse response) {
31 targetCookies
32 .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 }
40
41 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 }
48
49 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}
Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell my personal information
  • AdChoices
Copyright © 2022 Adobe. All rights reserved.