| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package com.xpn.xwiki.web; |
| 21 |
|
|
| 22 |
|
import java.io.File; |
| 23 |
|
import java.io.IOException; |
| 24 |
|
import java.io.UnsupportedEncodingException; |
| 25 |
|
import java.net.URI; |
| 26 |
|
import java.net.URLDecoder; |
| 27 |
|
import java.net.URLEncoder; |
| 28 |
|
import java.util.ArrayList; |
| 29 |
|
import java.util.List; |
| 30 |
|
import java.util.regex.Matcher; |
| 31 |
|
import java.util.regex.Pattern; |
| 32 |
|
|
| 33 |
|
import org.apache.commons.io.FileUtils; |
| 34 |
|
import org.apache.commons.io.IOUtils; |
| 35 |
|
import org.apache.commons.lang3.StringUtils; |
| 36 |
|
import org.apache.tika.Tika; |
| 37 |
|
import org.apache.tika.mime.MimeTypes; |
| 38 |
|
import org.slf4j.Logger; |
| 39 |
|
import org.slf4j.LoggerFactory; |
| 40 |
|
import org.xwiki.environment.Environment; |
| 41 |
|
|
| 42 |
|
import com.xpn.xwiki.XWikiContext; |
| 43 |
|
import com.xpn.xwiki.XWikiException; |
| 44 |
|
import com.xpn.xwiki.util.Util; |
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
@version |
| 61 |
|
@since |
| 62 |
|
@deprecated |
| 63 |
|
|
| 64 |
|
@Deprecated |
| |
|
| 93.3% |
Uncovered Elements: 4 (60) |
Complexity: 14 |
Complexity Density: 0.31 |
|
| 65 |
|
public class TempResourceAction extends XWikiAction |
| 66 |
|
{ |
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
public static final Pattern URI_PATTERN = Pattern.compile(".*?/temp/([^/]*+)/([^/]*+)/([^/]*+)/(.*+)"); |
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
private static final String PATH_SEPARATOR = "/"; |
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
private static final String URL_ENCODING = "UTF-8"; |
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(TempResourceAction.class); |
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 90 |
|
private Tika tika = new Tika(); |
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
private Environment environment = Utils.getComponent(Environment.class); |
| 96 |
|
|
| |
|
| 92% |
Uncovered Elements: 2 (25) |
Complexity: 5 |
Complexity Density: 0.24 |
|
| 97 |
10 |
@Override... |
| 98 |
|
public String render(XWikiContext context) throws XWikiException |
| 99 |
|
{ |
| 100 |
10 |
XWikiRequest request = context.getRequest(); |
| 101 |
10 |
XWikiResponse response = context.getResponse(); |
| 102 |
10 |
String uri = request.getRequestURI(); |
| 103 |
|
|
| 104 |
|
|
| 105 |
10 |
File tempFile = getTemporaryFile(uri, context); |
| 106 |
10 |
if (null == tempFile) { |
| 107 |
1 |
throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_URL_EXCEPTION, |
| 108 |
|
"Invalid temporary resource URL"); |
| 109 |
|
} |
| 110 |
|
|
| 111 |
|
|
| 112 |
9 |
response.setDateHeader("Last-Modified", tempFile.lastModified()); |
| 113 |
9 |
String contentType = MimeTypes.OCTET_STREAM; |
| 114 |
9 |
try { |
| 115 |
9 |
contentType = this.tika.detect(tempFile); |
| 116 |
|
} catch (IOException ex) { |
| 117 |
0 |
LOGGER.warn( |
| 118 |
|
String.format("Unable to determine mime type for temporary resource [%s]", tempFile.getAbsolutePath()), |
| 119 |
|
ex); |
| 120 |
|
} |
| 121 |
9 |
response.setContentType(contentType); |
| 122 |
9 |
if ("1".equals(request.getParameter("force-download"))) { |
| 123 |
1 |
String fileName = StringUtils.defaultIfBlank(request.getParameter("force-filename"), tempFile.getName()); |
| 124 |
1 |
fileName = Util.encodeURI(fileName, context).replaceAll("\\+", "%20"); |
| 125 |
1 |
response.addHeader("Content-disposition", "attachment; filename*=utf-8''" + fileName); |
| 126 |
|
} |
| 127 |
9 |
try { |
| 128 |
9 |
response.setContentLength((int) tempFile.length()); |
| 129 |
9 |
IOUtils.copy(FileUtils.openInputStream(tempFile), response.getOutputStream()); |
| 130 |
|
} catch (IOException e) { |
| 131 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_APP, |
| 132 |
|
XWikiException.ERROR_XWIKI_APP_SEND_RESPONSE_EXCEPTION, "Exception while sending response", e); |
| 133 |
|
} |
| 134 |
9 |
return null; |
| 135 |
|
} |
| 136 |
|
|
| 137 |
|
|
| 138 |
|
|
| 139 |
|
|
| 140 |
|
@param |
| 141 |
|
@param |
| 142 |
|
@return |
| 143 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (23) |
Complexity: 4 |
Complexity Density: 0.24 |
|
| 144 |
16 |
protected File getTemporaryFile(String uri, XWikiContext context)... |
| 145 |
|
{ |
| 146 |
16 |
Matcher matcher = URI_PATTERN.matcher(uri); |
| 147 |
16 |
File result = null; |
| 148 |
16 |
if (matcher.find()) { |
| 149 |
15 |
List<String> pathSegments = new ArrayList<String>(); |
| 150 |
|
|
| 151 |
15 |
pathSegments.add("temp"); |
| 152 |
|
|
| 153 |
15 |
pathSegments.add(withMinimalURLEncoding(matcher.group(3))); |
| 154 |
|
|
| 155 |
15 |
pathSegments.add(encodeURLPathSegment(context.getWikiId())); |
| 156 |
|
|
| 157 |
15 |
pathSegments.add(withMinimalURLEncoding(matcher.group(1))); |
| 158 |
|
|
| 159 |
15 |
pathSegments.add(withMinimalURLEncoding(matcher.group(2))); |
| 160 |
|
|
| 161 |
|
|
| 162 |
15 |
String prefix = StringUtils.join(pathSegments, PATH_SEPARATOR); |
| 163 |
|
|
| 164 |
15 |
for (String filePathSegment : matcher.group(4).split(PATH_SEPARATOR)) { |
| 165 |
18 |
pathSegments.add(withMinimalURLEncoding(filePathSegment)); |
| 166 |
|
} |
| 167 |
15 |
String path = URI.create(StringUtils.join(pathSegments, PATH_SEPARATOR)).normalize().toString(); |
| 168 |
15 |
if (path.startsWith(prefix)) { |
| 169 |
14 |
result = new File(this.environment.getTemporaryDirectory(), path); |
| 170 |
14 |
result = result.exists() ? result : null; |
| 171 |
|
} |
| 172 |
|
} |
| 173 |
16 |
return result; |
| 174 |
|
} |
| 175 |
|
|
| 176 |
|
|
| 177 |
|
|
| 178 |
|
|
| 179 |
|
|
| 180 |
|
|
| 181 |
|
|
| 182 |
|
|
| 183 |
|
|
| 184 |
|
@param |
| 185 |
|
@return |
| 186 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 187 |
63 |
private String withMinimalURLEncoding(String encodedPathSegment)... |
| 188 |
|
{ |
| 189 |
63 |
return encodeURLPathSegment(decodeURLPathSegment(encodedPathSegment)); |
| 190 |
|
} |
| 191 |
|
|
| |
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 192 |
78 |
private String encodeURLPathSegment(String segment)... |
| 193 |
|
{ |
| 194 |
78 |
try { |
| 195 |
78 |
return URLEncoder.encode(segment, URL_ENCODING); |
| 196 |
|
} catch (UnsupportedEncodingException e) { |
| 197 |
|
|
| 198 |
0 |
return segment; |
| 199 |
|
} |
| 200 |
|
} |
| 201 |
|
|
| |
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 202 |
63 |
private String decodeURLPathSegment(String encodedSegment)... |
| 203 |
|
{ |
| 204 |
63 |
try { |
| 205 |
63 |
return URLDecoder.decode(encodedSegment, URL_ENCODING); |
| 206 |
|
} catch (UnsupportedEncodingException e) { |
| 207 |
|
|
| 208 |
0 |
return encodedSegment; |
| 209 |
|
} |
| 210 |
|
} |
| 211 |
|
} |