1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.mobility.icons.controllers;
17
18 import org.apache.commons.io.IOUtils;
19 import org.kuali.mobility.icons.entity.WebIcon;
20 import org.kuali.mobility.icons.service.IconsService;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.beans.factory.annotation.Qualifier;
25 import org.springframework.stereotype.Controller;
26 import org.springframework.ui.Model;
27 import org.springframework.web.bind.annotation.PathVariable;
28 import org.springframework.web.bind.annotation.RequestMapping;
29
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import java.io.File;
33 import java.io.FileInputStream;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.util.List;
37
38
39
40
41
42
43 @Controller
44 public class WebIconsController {
45
46 private static final Logger LOG = LoggerFactory.getLogger(WebIconsController.class);
47
48
49
50
51 @Autowired
52 @Qualifier("iconsService")
53 private IconsService iconService;
54
55
56 @RequestMapping("css/icons.css")
57 public String getIconsCss(Model uiModel){
58 List<WebIcon> icons = iconService.getIcons();
59
60 uiModel.addAttribute("icons", icons);
61 return "icons/icons";
62 }
63
64
65
66
67
68
69 @RequestMapping("iconTest")
70 public String iconTest(Model uiModel){
71 List<WebIcon> icons = iconService.getIcons();
72
73 uiModel.addAttribute("icons", icons);
74 return "icons/iconTest";
75 }
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 @RequestMapping("getIcon/{iconName:[a-zA-Z]+}-{size:\\d+}")
91 public void getImageSize(
92 @PathVariable("iconName") String iconName,
93 @PathVariable("size") int size,
94 HttpServletRequest request,
95 HttpServletResponse response) throws IOException {
96
97 writeIconToHttpResponse(iconName, null, size, request, response);
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111
112 @RequestMapping("getIcon/{iconName:[a-zA-Z]+}-{theme:[a-zA-Z]+}-{size:\\d+}")
113 public void getImageThemeSize(
114 @PathVariable("iconName") String iconName,
115 @PathVariable("theme") String theme,
116 @PathVariable("size") int size,
117 HttpServletRequest request,
118 HttpServletResponse response) throws IOException {
119
120 writeIconToHttpResponse(iconName, theme, size, request, response);
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134
135 @RequestMapping("getIcon/{iconName:[a-zA-Z]+}-{size:\\d+}@{multiplier:\\d+}")
136 public void getIconWithMultiplier(
137 @PathVariable("iconName") String iconName,
138 @PathVariable("size") int size,
139 @PathVariable("multiplier") int multiplier,
140 HttpServletRequest request,
141 HttpServletResponse response) throws IOException {
142
143 writeIconToHttpResponse(iconName, null, (size * multiplier), request, response);
144 }
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160 @RequestMapping("getIcon/{iconName:[a-zA-Z]+}-{theme:[a-zA-Z]+}-{size:\\d+}@{multiplier:\\d+}")
161 public void getImageThemeWithMultiplier(
162 @PathVariable("iconName") String iconName,
163 @PathVariable("theme") String theme,
164 @PathVariable("size") int size,
165 @PathVariable("multiplier") int multiplier,
166 HttpServletRequest request,
167 HttpServletResponse response) throws IOException {
168
169 writeIconToHttpResponse(iconName, theme, (size * multiplier), request, response);
170 }
171
172
173
174
175
176
177
178
179
180
181
182 private void writeIconToHttpResponse(String iconName, String theme, int size, HttpServletRequest request, HttpServletResponse response) throws IOException{
183 long dateChanged = request.getDateHeader("If-Modified-Since") / 1000;
184 File imageFile = iconService.getImageFile(iconName, theme, size);
185
186 long mediaChanged = imageFile.lastModified() / 1000;
187 if (dateChanged == mediaChanged){
188 response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
189 return;
190 }
191 response.setContentType("image/png");
192 InputStream imageInput = new FileInputStream(imageFile);
193 response.setDateHeader("Last-Modified", imageFile.lastModified());
194
195 int bytesWritten = IOUtils.copy(imageInput, response.getOutputStream());
196 response.setContentLength(bytesWritten);
197 response.flushBuffer();
198 }
199
200
201
202 }