Skip to content

Commit dfed1c3

Browse files
committed
refactor(ImageUrl): extract exposeResourcesToView() method.
Addressed to #927 No functional changes.
1 parent 9e4ec9f commit dfed1c3

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

src/main/java/ru/mystamps/web/Url.java

+21-8
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,14 @@ public static Map<String, String> asMap(boolean production) {
141141
map.put("SUGGEST_SERIES_CATEGORY", SUGGEST_SERIES_CATEGORY);
142142

143143
if (serveContentFromSingleHost) {
144-
// Constants sorted in an ascending order.
144+
ImageUrl.exposeResourcesToView(map);
145+
145146
map.put("BOOTSTRAP_CSS", BOOTSTRAP_CSS);
146147
map.put("BOOTSTRAP_JS", BOOTSTRAP_JS);
147148
map.put("CATALOG_UTILS_JS", CATALOG_UTILS_JS);
148149
map.put("COLLECTION_INFO_JS", COLLECTION_INFO_JS);
149150
map.put("DATE_UTILS_JS", DATE_UTILS_JS);
150151
map.put("FAVICON_ICO", FAVICON_ICO);
151-
map.put("GET_IMAGE_PAGE", ImageUrl.GET_IMAGE_PAGE);
152-
map.put("GET_IMAGE_PREVIEW_PAGE", ImageUrl.GET_IMAGE_PREVIEW_PAGE);
153152
map.put("JQUERY_JS", JQUERY_JS);
154153
map.put("MAIN_CSS", MAIN_CSS);
155154
map.put("PARTICIPANT_ADD_JS", PARTICIPANT_ADD_JS);
@@ -158,15 +157,29 @@ public static Map<String, String> asMap(boolean production) {
158157
map.put("SERIES_ADD_JS", SERIES_ADD_JS);
159158
map.put("SERIES_INFO_JS", SERIES_INFO_JS);
160159
} else {
161-
// Use a separate domain for our own resources
162-
// Constants sorted in an ascending order.
160+
// This is a simplest decorator around Map that modifies inserted URLs by prepending
161+
// a host for static resources to them.
162+
//
163+
// I don't want to use ForwardingMap (Guava) or TransformedMap (commons-collections)
164+
// as we don't have them in dependencies and I don't want to add them either just for
165+
// a few lines of code.
166+
//
167+
// NOTE: this implementation won't work as expected when a caller uses putAll(),
168+
// putIfAbsent() or modifies a map by other ways.
169+
Map<String, String> resourcesMap = new HashMap<String, String>(map) {
170+
@Override
171+
public String put(String key, String value) {
172+
// Use a separate domain for our own resources
173+
return map.put(STATIC_RESOURCES_URL + key, value);
174+
}
175+
};
176+
177+
ImageUrl.exposeResourcesToView(resourcesMap);
178+
163179
map.put("CATALOG_UTILS_JS", STATIC_RESOURCES_URL + CATALOG_UTILS_JS);
164180
map.put("COLLECTION_INFO_JS", STATIC_RESOURCES_URL + COLLECTION_INFO_JS);
165181
map.put("DATE_UTILS_JS", STATIC_RESOURCES_URL + DATE_UTILS_JS);
166182
map.put("FAVICON_ICO", STATIC_RESOURCES_URL + FAVICON_ICO);
167-
map.put("GET_IMAGE_PAGE", STATIC_RESOURCES_URL + ImageUrl.GET_IMAGE_PAGE);
168-
// CheckStyle: ignore LineLength for next 1 line
169-
map.put("GET_IMAGE_PREVIEW_PAGE", STATIC_RESOURCES_URL + ImageUrl.GET_IMAGE_PREVIEW_PAGE);
170183
map.put("MAIN_CSS", STATIC_RESOURCES_URL + MAIN_CSS);
171184
map.put("PARTICIPANT_ADD_JS", STATIC_RESOURCES_URL + PARTICIPANT_ADD_JS);
172185
map.put("SERIES_ADD_JS", STATIC_RESOURCES_URL + SERIES_ADD_JS);

src/main/java/ru/mystamps/web/feature/image/ImageUrl.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,27 @@
1717
*/
1818
package ru.mystamps.web.feature.image;
1919

20+
import java.util.Map;
21+
2022
/**
2123
* Image-related URLs.
2224
*
2325
* Should be used everywhere instead of hard-coded paths.
2426
*
2527
* @author Slava Semushin
2628
*/
29+
@SuppressWarnings("PMD.CommentDefaultAccessModifier")
2730
public final class ImageUrl {
2831

29-
public static final String GET_IMAGE_PAGE = "/image/{id}";
30-
public static final String GET_IMAGE_PREVIEW_PAGE = "/image/preview/{id}";
32+
static final String GET_IMAGE_PAGE = "/image/{id}";
33+
static final String GET_IMAGE_PREVIEW_PAGE = "/image/preview/{id}";
3134

3235
private ImageUrl() {
3336
}
3437

38+
public static void exposeResourcesToView(Map<String, String> resources) {
39+
resources.put("GET_IMAGE_PAGE", GET_IMAGE_PAGE);
40+
resources.put("GET_IMAGE_PREVIEW_PAGE", GET_IMAGE_PREVIEW_PAGE);
41+
}
42+
3543
}

0 commit comments

Comments
 (0)