Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
OLAT CI-CD Testing Project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Lars Oliver Dam
OLAT CI-CD Testing Project
Commits
417aff44
Commit
417aff44
authored
13 years ago
by
srosse
Browse files
Options
Downloads
Patches
Plain Diff
OMA-54: return a real mime type with the download of forum's attachments
parent
e37fb05b
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/org/olat/modules/fo/restapi/ForumWebService.java
+7
-2
7 additions, 2 deletions
...ain/java/org/olat/modules/fo/restapi/ForumWebService.java
src/test/java/org/olat/restapi/ForumTest.java
+38
-7
38 additions, 7 deletions
src/test/java/org/olat/restapi/ForumTest.java
with
45 additions
and
9 deletions
src/main/java/org/olat/modules/fo/restapi/ForumWebService.java
+
7
−
2
View file @
417aff44
...
@@ -64,6 +64,7 @@ import org.olat.core.logging.OLog;
...
@@ -64,6 +64,7 @@ import org.olat.core.logging.OLog;
import
org.olat.core.logging.Tracing
;
import
org.olat.core.logging.Tracing
;
import
org.olat.core.util.FileUtils
;
import
org.olat.core.util.FileUtils
;
import
org.olat.core.util.StringHelper
;
import
org.olat.core.util.StringHelper
;
import
org.olat.core.util.WebappHelper
;
import
org.olat.core.util.vfs.LocalFileImpl
;
import
org.olat.core.util.vfs.LocalFileImpl
;
import
org.olat.core.util.vfs.VFSContainer
;
import
org.olat.core.util.vfs.VFSContainer
;
import
org.olat.core.util.vfs.VFSItem
;
import
org.olat.core.util.vfs.VFSItem
;
...
@@ -455,7 +456,9 @@ public class ForumWebService {
...
@@ -455,7 +456,9 @@ public class ForumWebService {
Response
.
ResponseBuilder
response
=
request
.
evaluatePreconditions
(
lastModified
);
Response
.
ResponseBuilder
response
=
request
.
evaluatePreconditions
(
lastModified
);
if
(
response
==
null
)
{
if
(
response
==
null
)
{
File
attachment
=
((
LocalFileImpl
)
item
).
getBasefile
();
File
attachment
=
((
LocalFileImpl
)
item
).
getBasefile
();
response
=
Response
.
ok
(
attachment
).
lastModified
(
lastModified
).
cacheControl
(
cc
);
String
mimeType
=
WebappHelper
.
getMimeType
(
attachment
.
getName
());
if
(
mimeType
==
null
)
mimeType
=
"application/octet-stream"
;
response
=
Response
.
ok
(
attachment
).
lastModified
(
lastModified
).
type
(
mimeType
).
cacheControl
(
cc
);
}
}
return
response
.
build
();
return
response
.
build
();
}
else
if
(
item
instanceof
VFSLeaf
)
{
}
else
if
(
item
instanceof
VFSLeaf
)
{
...
@@ -464,7 +467,9 @@ public class ForumWebService {
...
@@ -464,7 +467,9 @@ public class ForumWebService {
Response
.
ResponseBuilder
response
=
request
.
evaluatePreconditions
(
lastModified
);
Response
.
ResponseBuilder
response
=
request
.
evaluatePreconditions
(
lastModified
);
if
(
response
==
null
)
{
if
(
response
==
null
)
{
StreamingOutput
attachment
=
new
VFSStreamingOutput
((
VFSLeaf
)
item
);
StreamingOutput
attachment
=
new
VFSStreamingOutput
((
VFSLeaf
)
item
);
response
=
Response
.
ok
(
attachment
).
lastModified
(
lastModified
).
cacheControl
(
cc
);
String
mimeType
=
WebappHelper
.
getMimeType
(
item
.
getName
());
if
(
mimeType
==
null
)
mimeType
=
"application/octet-stream"
;
response
=
Response
.
ok
(
attachment
).
lastModified
(
lastModified
).
type
(
mimeType
).
cacheControl
(
cc
);
}
}
return
response
.
build
();
return
response
.
build
();
}
}
...
...
This diff is collapsed.
Click to expand it.
src/test/java/org/olat/restapi/ForumTest.java
+
38
−
7
View file @
417aff44
...
@@ -39,6 +39,7 @@ import java.net.URI;
...
@@ -39,6 +39,7 @@ import java.net.URI;
import
java.net.URISyntaxException
;
import
java.net.URISyntaxException
;
import
java.net.URL
;
import
java.net.URL
;
import
java.util.List
;
import
java.util.List
;
import
java.util.UUID
;
import
javax.imageio.ImageIO
;
import
javax.imageio.ImageIO
;
import
javax.ws.rs.core.MediaType
;
import
javax.ws.rs.core.MediaType
;
...
@@ -56,6 +57,8 @@ import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
...
@@ -56,6 +57,8 @@ import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import
org.apache.commons.httpclient.methods.multipart.Part
;
import
org.apache.commons.httpclient.methods.multipart.Part
;
import
org.apache.commons.httpclient.methods.multipart.StringPart
;
import
org.apache.commons.httpclient.methods.multipart.StringPart
;
import
org.apache.commons.io.IOUtils
;
import
org.apache.commons.io.IOUtils
;
import
org.apache.http.HttpResponse
;
import
org.apache.http.client.methods.HttpGet
;
import
org.codehaus.jackson.map.ObjectMapper
;
import
org.codehaus.jackson.map.ObjectMapper
;
import
org.codehaus.jackson.type.TypeReference
;
import
org.codehaus.jackson.type.TypeReference
;
import
org.junit.Before
;
import
org.junit.Before
;
...
@@ -76,6 +79,7 @@ import org.olat.restapi.support.vo.File64VO;
...
@@ -76,6 +79,7 @@ import org.olat.restapi.support.vo.File64VO;
import
org.olat.restapi.support.vo.FileVO
;
import
org.olat.restapi.support.vo.FileVO
;
import
org.olat.test.JunitTestHelper
;
import
org.olat.test.JunitTestHelper
;
import
org.olat.test.OlatJerseyTestCase
;
import
org.olat.test.OlatJerseyTestCase
;
import
org.springframework.beans.factory.annotation.Autowired
;
public
class
ForumTest
extends
OlatJerseyTestCase
{
public
class
ForumTest
extends
OlatJerseyTestCase
{
...
@@ -83,6 +87,9 @@ public class ForumTest extends OlatJerseyTestCase {
...
@@ -83,6 +87,9 @@ public class ForumTest extends OlatJerseyTestCase {
private
static
Message
m1
,
m2
,
m3
,
m4
,
m5
;
private
static
Message
m1
,
m2
,
m3
,
m4
,
m5
;
private
static
Identity
id1
;
private
static
Identity
id1
;
@Autowired
private
ForumManager
forumManager
;
@Before
@Before
public
void
setUp
()
throws
Exception
{
public
void
setUp
()
throws
Exception
{
super
.
setUp
();
super
.
setUp
();
...
@@ -248,15 +255,39 @@ public class ForumTest extends OlatJerseyTestCase {
...
@@ -248,15 +255,39 @@ public class ForumTest extends OlatJerseyTestCase {
@Test
@Test
public
void
testGetAttachment
()
throws
IOException
,
URISyntaxException
{
public
void
testGetAttachment
()
throws
IOException
,
URISyntaxException
{
HttpClient
c
=
loginWithCookie
(
"administrator"
,
"openolat"
);
//set a attachment
VFSContainer
container
=
forumManager
.
getMessageContainer
(
m1
.
getForum
().
getKey
(),
m1
.
getKey
());
InputStream
portraitIn
=
CoursesElementsTest
.
class
.
getResourceAsStream
(
"portrait.jpg"
);
assertNotNull
(
portraitIn
);
VFSLeaf
attachment
=
container
.
createChildLeaf
(
UUID
.
randomUUID
().
toString
().
replace
(
"-"
,
""
)
+
".jpg"
);
FileUtils
.
bcopy
(
portraitIn
,
attachment
.
getOutputStream
(
false
),
""
);
RestConnection
conn
=
new
RestConnection
();
assertTrue
(
conn
.
login
(
"administrator"
,
"openolat"
));
URI
uri
=
getForumUriBuilder
().
path
(
"posts"
).
path
(
m1
.
getKey
().
toString
()).
path
(
"attachments"
).
build
();
URI
uri
=
getForumUriBuilder
().
path
(
"posts"
).
path
(
m1
.
getKey
().
toString
()).
path
(
"attachments"
).
build
();
GetMethod
method
=
createGet
(
uri
,
MediaType
.
APPLICATION_JSON
,
true
);
HttpGet
method
=
conn
.
createGet
(
uri
,
MediaType
.
APPLICATION_JSON
,
true
);
int
code
=
c
.
executeMethod
(
method
);
HttpResponse
response
=
conn
.
execute
(
method
);
assertEquals
(
200
,
code
);
assertEquals
(
200
,
response
.
getStatusLine
().
getStatusCode
());
InputStream
body
=
method
.
getResponseBodyAsStream
();
List
<
FileVO
>
files
=
parseFileArray
(
response
.
getEntity
().
getContent
());
List
<
FileVO
>
files
=
parseFileArray
(
body
);
assertNotNull
(
files
);
assertNotNull
(
files
);
FileVO
attachmentVO
=
null
;
for
(
FileVO
file:
files
)
{
if
(
attachment
.
getName
().
equals
(
file
.
getTitle
()))
{
attachmentVO
=
file
;
}
}
assertNotNull
(
attachmentVO
);
URI
downloadURI
=
new
URI
(
attachmentVO
.
getHref
());
HttpGet
download
=
conn
.
createGet
(
downloadURI
,
MediaType
.
APPLICATION_JSON
,
true
);
HttpResponse
downloadResponse
=
conn
.
execute
(
download
);
assertEquals
(
200
,
downloadResponse
.
getStatusLine
().
getStatusCode
());
String
contentType
=
downloadResponse
.
getEntity
().
getContentType
().
getValue
();
assertEquals
(
"image/jpeg"
,
contentType
);
}
}
@Test
@Test
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment