Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
brainfood
/
ofbiz-directcontrolservlet
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
0f8e94b0
authored
2014-09-04 12:58:43 -0500
by
Ean Schuessler
Browse Files
Options
Browse Files
Tag
Download
Plain Diff
Merge branch 'BF-3872' of /home/git/repositories/brainfood/ofbiz-directcontrolservlet
2 parents
d7b2fc8d
cf441efb
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
44 additions
and
51 deletions
src/com/brainfood/ofbiz/DirectControlServlet.java
src/com/brainfood/ofbiz/DirectControlServlet.java
View file @
0f8e94b
...
...
@@ -40,6 +40,7 @@ import org.ofbiz.base.util.StringUtil;
import
org.ofbiz.base.util.UtilHttp
;
import
org.ofbiz.base.util.UtilMisc
;
import
org.ofbiz.base.util.UtilProperties
;
import
org.ofbiz.base.util.UtilIO
;
import
org.ofbiz.entity.Delegator
;
import
org.ofbiz.entity.DelegatorFactory
;
...
...
@@ -90,62 +91,64 @@ public class DirectControlServlet extends HttpServlet {
}
}
public
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
doPost
(
request
,
response
);
}
public
void
doPost
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
String
method
=
""
;
Debug
.
logInfo
(
"getPathInfo: "
+
request
.
getPathInfo
()
+
" request.getContentType: "
+
request
.
getContentType
(),
module
);
String
pathInfo
=
request
.
getPathInfo
();
if
(
pathInfo
==
null
||
pathInfo
.
length
()
==
0
)
{
return
;
}
pathInfo
=
pathInfo
.
substring
(
1
).
replaceAll
(
":"
,
"."
);
public
void
service
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
try
{
De
legator
delegator
=
getDelegator
(
request
.
getServletContext
());
LocalDispatcher
dispatcher
=
getDispatcher
(
request
.
getServletContext
()
);
De
bug
.
logInfo
(
"getPathInfo: "
+
request
.
getPathInfo
()
+
" request.getContentType: "
+
request
.
getContentType
(),
module
);
// Build context
Map
<
String
,
Object
>
context
=
new
HashMap
<
String
,
Object
>();
String
paramList
=
""
;
for
(
Iterator
<
String
>
k
=
request
.
getParameterMap
().
keySet
().
iterator
();
k
.
hasNext
();
)
{
paramList
+=
" "
+
k
.
next
();
String
pathInfo
=
request
.
getPathInfo
();
if
(
pathInfo
==
null
||
pathInfo
.
length
()
==
0
)
{
return
;
}
pathInfo
=
pathInfo
.
substring
(
1
).
replaceAll
(
":"
,
"."
);
Debug
.
logInfo
(
"PARAMETERS: "
+
paramList
,
module
);
//
Handle if this is a form post
// Determine type of request and load the context from JSON content or
//
parameter values accordingly
String
contentType
=
request
.
getContentType
();
// Determine the request method for service lookup and parameter filters
String
method
=
""
;
method
=
request
.
getParameter
(
"_method"
);
String
httpMethod
=
request
.
getMethod
();
if
(
method
==
null
&&
httpMethod
!=
null
)
method
=
httpMethod
;
Debug
.
logInfo
(
"Method: HTTP("
+
httpMethod
+
") embedded("
+
method
+
")"
,
module
);
if
(
method
==
null
)
method
=
"GET"
;
// If this is a backbone PUT/DELETE emulated call then handle it
if
(
request
.
getParameter
(
"model"
)
!=
null
)
{
// if (!"CALL".equals(method)) context.put("_method", method);
JSON
json
=
new
JSON
(
new
StringReader
(
request
.
getParameter
(
"model"
)));
Debug
.
logInfo
(
"Method: HTTP("
+
httpMethod
+
") embedded("
+
method
+
")"
,
module
);
// Load context
Map
<
String
,
Object
>
context
=
new
HashMap
<
String
,
Object
>();
if
(
"application/json"
.
equals
(
contentType
))
{
// Read request body as JSON and insert into the context
JSON
json
=
new
JSON
(
new
InputStreamReader
(
request
.
getInputStream
()));
Map
<
String
,
Object
>
items
=
json
.
JSONObject
();
for
(
String
key
:
items
.
keySet
())
{
if
(!
"sessionId"
.
equals
(
key
)
&&
!
"_method"
.
equals
(
key
))
{
context
.
put
(
key
,
items
.
get
(
key
));
Debug
.
logInfo
(
">parameter '"
+
key
+
"'="
+
items
.
get
(
key
),
module
);
}
context
.
put
(
key
,
items
.
get
(
key
));
}
}
else
{
for
(
Enumeration
<
String
>
params
=
request
.
getParameterNames
();
params
.
hasMoreElements
();)
{
String
param
=
params
.
nextElement
();
Object
[]
values
=
request
.
getParameterValues
(
param
);
if
(!
"sessionId"
.
equals
(
param
)
&&
!
"_method"
.
equals
(
param
))
{
context
.
put
(
param
,
values
.
length
==
1
?
values
[
0
]
:
values
);
Debug
.
logInfo
(
"!parameter '"
+
param
+
"'="
+
context
.
get
(
param
),
module
);
// Check if the request is a backbone style "emulateJSON" request
if
(
request
.
getContentType
()
==
"application/x-www-form-urlencoded"
&&
request
.
getParameter
(
"model"
)
!=
null
)
{
JSON
json
=
new
JSON
(
new
StringReader
(
request
.
getParameter
(
"model"
)));
Map
<
String
,
Object
>
items
=
json
.
JSONObject
();
for
(
String
key
:
items
.
keySet
())
{
if
(!
"sessionId"
.
equals
(
key
))
{
context
.
put
(
key
,
items
.
get
(
key
));
}
}
}
else
{
// Directly copy request parameters into context.
for
(
Enumeration
<
String
>
params
=
request
.
getParameterNames
();
params
.
hasMoreElements
();)
{
String
param
=
params
.
nextElement
();
Object
[]
values
=
request
.
getParameterValues
(
param
);
if
(!
"sessionId"
.
equals
(
param
)
&&
!
"_method"
.
equals
(
param
))
{
context
.
put
(
param
,
values
.
length
==
1
?
values
[
0
]
:
values
);
}
}
}
}
Delegator
delegator
=
getDelegator
(
request
.
getServletContext
());
LocalDispatcher
dispatcher
=
getDispatcher
(
request
.
getServletContext
());
// If there is a mapping for this pathInfo, run the corresponding service
// otherwise, return an error
String
serviceName
=
serviceURLMappings
.
get
(
pathInfo
+
"#"
+
method
);
...
...
@@ -153,7 +156,7 @@ public class DirectControlServlet extends HttpServlet {
if
(
serviceName
==
null
)
{
serviceName
=
serviceURLMappings
.
get
(
pathInfo
);
if
(
serviceName
==
null
)
{
response
.
setStatus
(
40
0
);
response
.
setStatus
(
40
4
);
Debug
.
logInfo
(
"No mapping found for "
+
pathInfo
+
"#"
+
method
,
module
);
...
...
@@ -200,17 +203,8 @@ public class DirectControlServlet extends HttpServlet {
Locale
locale
=
UtilHttp
.
getLocale
(
request
);
TimeZone
timeZone
=
UtilHttp
.
getTimeZone
(
request
);
/*
DispatchContext dctx = dispatcher.getDispatchContext();
ModelService model = dctx.getModelService(serviceName);
for (ModelParam modelParam: model.getInModelParamList()) {
}
*/
Map
<
String
,
Object
>
result
=
dispatcher
.
runSync
(
serviceName
,
context
);
// System.err.println("RESULT:" + result);
result
.
remove
(
"responseMessage"
);
if
(
result
.
get
(
"errorMessage"
)
!=
null
)
{
...
...
@@ -225,12 +219,11 @@ public class DirectControlServlet extends HttpServlet {
String
jsonStr
=
json
.
toString
();
response
.
setContentLength
(
jsonStr
.
getBytes
(
"UTF8"
).
length
);
writer
.
write
(
jsonStr
);
// Debug.logWarning("JSON result [%s]", module, jsonStr);
writer
.
flush
();
writer
.
close
();
}
catch
(
Throwable
t
)
{
response
.
setStatus
(
4
00
);
response
.
setStatus
(
5
00
);
PrintWriter
writer
=
response
.
getWriter
();
Debug
.
logInfo
(
"Exception processing request"
,
module
);
Debug
.
logInfo
(
t
,
module
);
...
...
Please
register
or
sign in
to post a comment