Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
unstable
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository 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
PROINFODATA
unstable
Commits
ae743ef3
Commit
ae743ef3
authored
11 years ago
by
Eduardo L. Buratti
Browse files
Options
Downloads
Patches
Plain Diff
Modify SaveConfig controller to save files in DB (using models.SavedConfig)
parent
4d3e5c97
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
web/app/controllers/SaveConfig.java
+28
-41
28 additions, 41 deletions
web/app/controllers/SaveConfig.java
web/app/models/SavedConfig.java
+89
-0
89 additions, 0 deletions
web/app/models/SavedConfig.java
with
117 additions
and
41 deletions
web/app/controllers/SaveConfig.java
+
28
−
41
View file @
ae743ef3
...
...
@@ -13,89 +13,76 @@ import models.*;
public
class
SaveConfig
extends
Controller
{
private
static
String
TMP_DIR
=
"/tmp/"
;
private
static
long
MAX_FILE_LENGTH
=
10
*
1024
*
1024
;
public
static
Result
get
(
String
inep
,
String
macaddr
)
{
try
{
if
(!
School
.
checkMachineBelongsToSchool
(
inep
,
macaddr
))
return
forbidden
();
return
forbidden
(
"You do not have permission to access this file."
);
}
catch
(
java
.
sql
.
SQLException
e
)
{
e
.
printStackTrace
();
return
internalServerError
();
return
internalServerError
(
"Authentication failure."
);
}
String
filename
=
"config_"
+
inep
+
".bz2"
;
File
f
=
new
File
(
TMP_DIR
+
filename
);
String
filename
=
"config_"
+
inep
+
".
tar.
bz2"
;
InputStream
in
=
SavedConfig
.
get
(
inep
);
if
(
f
.
exists
())
{
response
().
setHeader
(
"Content-Disposition"
,
"attachment; filename="
+
filename
);
return
ok
(
f
);
}
else
if
(
in
==
null
)
return
notFound
();
response
().
setHeader
(
"Content-Disposition"
,
"attachment; filename="
+
filename
);
return
ok
(
in
);
}
public
static
Result
put
(
String
inep
,
String
macaddr
)
{
try
{
if
(!
School
.
checkMachineBelongsToSchool
(
inep
,
macaddr
))
return
forbidden
();
return
forbidden
(
"You do not have permission to access this file."
);
}
catch
(
java
.
sql
.
SQLException
e
)
{
e
.
printStackTrace
();
return
internalServerError
();
return
internalServerError
(
"Authentication failure."
);
}
String
filename
=
"config_"
+
inep
+
".bz2"
;
File
f
=
new
File
(
TMP_DIR
+
filename
);
InputStream
src
=
null
;
OutputStream
dst
=
null
;
try
{
if
(!
f
.
exists
())
{
f
.
createNewFile
();
}
MultipartFormData
body
=
request
().
body
().
asMultipartFormData
();
FilePart
uploadPart
=
body
.
getFile
(
"config"
);
if
(
(
uploadPart
==
null
)
||
(
uploadPart
.
getFile
()
==
null
))
throw
new
Exception
(
"
f
ailed to
get file
"
);
if
(
uploadPart
==
null
)
throw
new
Exception
(
"
F
ailed to
upload configuration.
"
);
src
=
new
FileInputStream
(
uploadPart
.
getFile
());
dst
=
new
FileOutputStream
(
f
);
File
uploadFile
=
uploadPart
.
getFile
();
if
(
uploadFile
==
null
)
throw
new
Exception
(
"Failed to upload configuration."
);
byte
[]
buf
=
new
byte
[
4096
];
int
len
;
while
((
len
=
src
.
read
(
buf
))
>
0
)
{
dst
.
write
(
buf
,
0
,
len
);
}
if
(
uploadFile
.
length
()
>
MAX_FILE_LENGTH
)
throw
new
Exception
(
"Configuration file is too large (>10MB)."
);
InputStream
src
=
new
FileInputStream
(
uploadFile
);
src
.
close
();
dst
.
close
(
);
if
(
!
SavedConfig
.
put
(
inep
,
src
,
uploadFile
.
length
())
)
throw
new
Exception
(
"Failure to save configuration to the database."
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
internalServerError
();
return
internalServerError
(
e
.
getMessage
()
);
}
return
ok
(
"config saved"
);
return
ok
(
);
// no news is good news
}
public
static
Result
exists
(
String
inep
,
String
macaddr
)
{
try
{
if
(!
School
.
checkMachineBelongsToSchool
(
inep
,
macaddr
))
return
forbidden
();
return
forbidden
(
"You do not have permission to access this file."
);
}
catch
(
java
.
sql
.
SQLException
e
)
{
e
.
printStackTrace
();
return
internalServerError
();
return
internalServerError
(
"Authentication failure."
);
}
String
filename
=
"config_"
+
inep
+
".bz2"
;
File
f
=
new
File
(
TMP_DIR
+
filename
);
if
(
f
.
exists
())
if
(
SavedConfig
.
exists
(
inep
))
return
ok
(
"true"
);
else
return
ok
(
"false"
);
...
...
This diff is collapsed.
Click to expand it.
web/app/models/SavedConfig.java
0 → 100644
+
89
−
0
View file @
ae743ef3
package
models
;
import
java.io.InputStream
;
import
java.util.ArrayList
;
import
java.sql.*
;
import
play.db.*
;
import
play.libs.Json
;
import
play.cache.Cache
;
import
org.codehaus.jackson.node.*
;
public
class
SavedConfig
implements
java
.
io
.
Serializable
{
private
static
final
long
serialVersionUID
=
19156549873156770L
;
public
static
boolean
put
(
String
inep
,
InputStream
in
,
long
length
)
{
Connection
conn
=
DB
.
getConnection
(
"le_save_config"
);
PreparedStatement
st
;
try
{
st
=
conn
.
prepareStatement
(
"DELETE FROM school_config WHERE inep=?"
);
st
.
setString
(
1
,
inep
);
st
.
executeUpdate
();
st
=
conn
.
prepareStatement
(
"INSERT INTO school_config (inep, config) VALUES (?,?);"
);
st
.
setString
(
1
,
inep
);
st
.
setBinaryStream
(
2
,
in
,
(
int
)
length
);
st
.
executeUpdate
();
st
.
close
();
conn
.
close
();
return
true
;
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
return
false
;
}
}
public
static
InputStream
get
(
String
inep
)
{
Connection
conn
=
DB
.
getConnection
(
"le_save_config"
);
PreparedStatement
st
;
InputStream
in
=
null
;
try
{
st
=
conn
.
prepareStatement
(
"SELECT config FROM school_config WHERE inep=?;"
);
st
.
setString
(
1
,
inep
);
ResultSet
res
=
st
.
executeQuery
();
if
(
res
.
next
())
in
=
res
.
getBinaryStream
(
1
);
st
.
close
();
conn
.
close
();
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
return
in
;
}
public
static
boolean
exists
(
String
inep
)
{
Connection
conn
=
DB
.
getConnection
(
"le_save_config"
);
PreparedStatement
st
;
boolean
ret
=
false
;
try
{
st
=
conn
.
prepareStatement
(
"SELECT 1 FROM school_config WHERE inep=?;"
);
st
.
setString
(
1
,
inep
);
ResultSet
res
=
st
.
executeQuery
();
if
(
res
.
next
())
ret
=
true
;
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
return
ret
;
}
}
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