I would like to check if a user is allowed to start a flow by checking if parameters are valid. If not I will send an error 404. But in the webflow de error response won't be send.
The webflow always returns status 200.
Does somebody have a solution for this problem?
def shortShopFlow = {
onStart {
log.info("starting a new shoppingCart traject")
Boolean isValidShop = shopService.isValidShop(params.code as String, params.token as String)
sessionFactory.currentSession.clear()
if (!params.code || !isValidShop) {
response.sendError(404)
return
}
}
You can do something:
MyController{
def shortShopFlow = {
onStart {
log.info("starting a new shoppingCart traject")
Boolean isValidShop = shopService.isValidShop(params.code as String, params.token as String)
sessionFactory.currentSession.clear()
if (!params.code || !isValidShop) {
return 404();
}
on('404').to ('pageNotFound');
}
pageNotFound{
redirect(action: 'fourZeroFour');
}
def fourZeroFour = {
response.sendError(404);
}
}
Hope this helps.
Minhaj
Related
I know that this question has many questions around in Stackoverflow but even though it has many answers about it, thoses answers couldn't help me at all and either figured it out yet.
In my WebAPP it works normal, but when I transform it to an API it fails(topic title error)... I have tried following:
save first Test(flush:true) // error the C must be NOT NULL
save testInstance.addToAs(a).save() //b and c
Why I can't save the whole object?
Domain Water
static hasMany = [personWaters : Person,
equipmentWaters: Equipment,
sampleWaters : WaterSample]
Domain WaterSample
static belongsTo = [water: Water]
Controller refactored some parts
def save() {
def waterParams = request.JSON
def sampleList = [] as Set
def equipmentList = [] as Set
def personList = [] as Set
waterAPIService.personPopulate(waterParams.personWaters, personList)
waterAPIService.equipmentPopulate(waterParams.equipmentWaters, equipmentList)
waterAPIService.samplePopulate(waterParams.sampleWaters, sampleList)
Date validity = Date.parse("yyyy-MM-dd'T'HH:mm:ss'Z'", waterParams.validity)
Date sample = Date.parse("yyyy-MM-dd'T'HH:mm:ss'Z'", waterParams.sampleDate)
waterParams.remove('personWaters')
waterParams.remove('equipmentWaters')
waterParams.remove('sampleWaters')
waterParams.remove('validity')
waterParams.remove('sampleDate')
waterParams.put('personWaters', personList)
waterParams.put('equipmentWaters', equipmentList)
waterParams.put('sampleWaters', sampleList)
waterParams.put('validity', validity)
waterParams.put('sampleDate', sample)
def waterInstance = new Water(waterParams)
//def test = waterAPIService.sampleValidated(sampleList ? sampleList.size() : 0, sampleList)
def test = 99
if (test == 99) {
if (saveTest(waterInstance)) {
render([message: ['ok']] as JSON)
} else {
def errors = []
errors.add('Erro ao salvar')
errors = waterInstance.errors.allErrors.collect { g.message([error: it]) }
render([message: errors] as JSON)
}
} else {
render render([message: ["Critério de estabilização não atendido para o parâmetro " + waterAPIService.messageSampleValidated(test)]] as JSON)
}
}
def saveTest(Water waterInstance) {
try {
println waterInstance.validate()
if (!waterInstance.hasErrors() && waterInstance.save() && waterInstance.validate()) {
return true
} else {
log.info("error", waterInstance.errors)
return false
}
} catch (e) {
log.info("exception error", e)
}
}
Service
def personPopulate(personWaters, personList) {
personWaters.each {
personList << Person.get(it.id)
}
return personList
}
def equipmentPopulate(equipmentWaters, equipmentList) {
equipmentWaters.each {
equipmentList << Equipment.get(it.id)
}
return equipmentList
}
def samplePopulate(sampleWaters, sampleList) {
sampleWaters.each {
def sample = new WaterSample()
sample.dateTime = Date.parse("yyyy-MM-dd'T'HH:mm:ss", it.dateTime)
sample.levelWater = it.levelWater
sample.conductivity = it.conductivity
sample.dissolvedOxygen = it.dissolvedOxygen
sample.redoxPotential = it.redoxPotential
sample.ph = it.ph
sample.temperature = it.temperature
sampleList << sample
}
return sampleList
}
waterParam JSON
{
"climateCondition":"SUNNY",
"equipmentWaters":[
{
"id":2
},
{
"id":4
}
],
"personWaters":[
{
"id":86
},
{
"id":143
}
],
"sampleWaters":[
{
"ReportId":2,
"conductivity":0,
"IDWeb":0,
"dissolvedOxygen":0,
"dateTime":"2015-12-07T17:08:00Z",
"levelWater":0,
"ID":4,
"UpdateDate":"0001-01-01T00:00:00Z",
"ph":0,
"redoxPotential":0,
"temperature":0
},
{
"ReportId":2,
"conductivity":0,
"IDWeb":0,
"dissolvedOxygen":0,
"dateTime":"2015-12-07T17:09:00Z",
"levelWater":0,
"ID":5,
"UpdateDate":"0001-01-01T00:00:00Z",
"ph":0,
"redoxPotential":0,
"temperature":0
},
{
"ReportId":2,
"conductivity":0,
"IDWeb":0,
"dissolvedOxygen":0,
"dateTime":"2015-12-07T18:13:00Z",
"levelWater":0,
"ID":6,
"UpdateDate":"0001-01-01T00:00:00Z",
"ph":0,
"redoxPotential":0,
"temperature":0
}
]
}
Ok, this was a headache to find one of the solution:
If you trying to save an object, debug your it and inspect any of nested objects, for example: I'm sending approval.id: null, the object will give me below:
"approval":{
"class":"User",
"id":null
},
Which is trying to add a user with id null, whick wont find and wont save. So remove it from your json request.
I'm getting this error in my spray project.
Error:(41, 28) type mismatch;
found : spray.routing.authentication.ContextAuthenticator[co.s4n.authentication.entities.Usuario]
(which expands to) spray.routing.RequestContext => scala.concurrent.Future[scala.util.Either[spray.routing.Rejection,co.s4n.authentication.entities.Usuario]]
required: spray.routing.directives.AuthMagnet[?]
authenticate(validateToken) {
^
This is my TokenValidator trait
trait TokenValidator {
def validateToken: ContextAuthenticator[Usuario] = {
ctx =>
val header = ctx.request.headers.find(_.name == "Access_Token")
if (header isDefined) {
doAuth(header.get)
}
else {
Future(Left(AuthenticationFailedRejection(AuthenticationFailedRejection.CredentialsMissing, List())))
}
}
def doAuth(header: HttpHeader): Future[Authentication[Usuario]] = {
Dao.validateToken(header.value).map {
case Some(usuario) => Right(usuario)
case None => Left(AuthenticationFailedRejection(AuthenticationFailedRejection.CredentialsRejected, List()))
}
}
}
and this is the line where I¡m getting that error
//#DELETE
//localhost:9090/authenticacion/users/{{userEmail}}
val `users/{{email}}` =
pathPrefix(`path-prefix`) {
pathPrefix(`users-path-prefix` / Segment) {
emailRef => {
delete {
authenticate(validateToken) { **HERE!!!!**
usuario =>
.....
}
}
}
}
}
Does anyone know what am I doing wrong?
Thak you all in advance!
The only thing I was missing was to have ExecutionContext in scope and import ExecutionContext.Implicits.global worked fine.
It's to let Futures work as they declare an implicit ExecutionContext parameter.
I know it's a long time since the actual question came, but the way to go with this for Spray is to determine the execution context with the tools that Spray provides:
implicit def executionContext = actorRefFactory.dispatcher
I'm using grails 1.3.7.
I have the following filter setup:
class MyFilters {
def userService
def springSecurityService
def filters = {
all(controller: '*', action: '*') {
before = {
String userAgent = request.getHeader('User-Agent')
int buildVersion = 0
// Match "app-/{version}" where {version} is the build number
def matcher = userAgent =~ "(?i)app(?:-\\w+)?\\/(\\d+)"
if (matcher.getCount() > 0)
{
buildVersion = Integer.parseInt(matcher[0][1])
log.info("User agent is from a mobile with build version = " + buildVersion)
log.info("User agent = " + userAgent)
String redirectUrl = "https://anotherdomain.com"
if (buildVersion > 12)
{
if (request.queryString != null)
{
log.info("Redirecting request to anotherdomain with query string")
redirect(url:"${redirectUrl}${request.forwardURI}?${request.queryString}",params:params)
}
return
}
}
}
after = { model ->
if (model) {
model['currentUser'] = userService.currentUser
}
}
afterView = {
}
}
}
}
A problem occurs in that the redirect does not happen at the point I would have thought.
I want all execution to stop and redirect to the exaact url I have given it at this point.
When i debug to the "redirect" line, it continues past this line exectuting other lines and jumping to another controller.
In order to prevent the normal processing flow from continuing, you need to return false from your before filter:
if (buildVersion > 12)
{
if (request.queryString != null)
{
log.info("Redirecting request to anotherdomain with query string")
redirect(url:"${redirectUrl}${request.forwardURI}?${request.queryString}",params:params)
return false
}
}
This is mentioned in passing at the very end of section 6.6.2 of the user guide, but it isn't particularly prominent:
Note how returning false ensure that the action itself is not executed.
I am getting a wierd problem and i am stuck on it for atleast 4 hours now. Actually i had written my code in a controller for testing but when i have moved the code to service i am getting a strange behaviour that the methods in service are not returning or may be methods that are calling them in the service only are not receiving .
class FacebookService implements InitializingBean, GroovyInterceptable {
def getUserLikes(def at){
List<String> listOfUrls = []
String basicFbUrl = "https://graph.facebook.com/"
String likeUrl = basicFbUrl + "me/likes?access_token=${at}"
URL url = new URL(likeUrl)
String jsonResponse = getResponseFromUrl(url)
println "JSON RESPONSE IS ${jsonResponse}" // this is showing null
}
String getResponseFromUrl() {
String something
String resp = null;
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
try {
int respCode = conn.responseCode
if (respCode == 400) {
log.error("COULD NOT MAKE CONNECTION")
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
def jsonResp = JSON.parse(br.text)
} else {
resp = conn.getInputStream().getText()
}
} finally {
conn.disconnect()
}
println("RETURNIG RESPONSE ${resp}") // This returns me a map as expected
return resp;
}
Dont know where does resp goes ?? any suggestions please ??
OK i know the culprit , i am posting the code of invokeMethod
def invokeMethod(String name, args){
System.out.println("IN INVOKE METHOD NAME ${name}")
if(facebookPalsCache==null)
facebookPalsCache = new FacebookPalsCache(1000)
System.out.println("time before ${name} called: ${new Date()}")
//Get the method that was originally called.
def calledMethod = metaClass.getMetaMethod(name, args)
System.out.println("CALLED METHOD IS ${calledMethod}")
//The "?" operator first checks to see that the "calledMethod" is not
//null (i.e. it exists).
if(name.equals("getFriends")){
println "getFriends..."
def session = RequestContextHolder.currentRequestAttributes().getSession()
def friends = facebookPalsCache.get(session.facebook.uid)
if(!friends){
def getFriends = facebookGraphService.invokeMethod (name, args)
println "Saving FBFRIENDS in CACHE"
facebookPalsCache.put(session.facebook.uid, getFriends)
return getFriends
}
else return friends
}
else {
if(calledMethod){
System.out.println("IN IF AND INVOKING METHOD ${calledMethod}")
calledMethod.invoke(this, args)
}
else {
return facebookGraphService.invokeMethod(name, args)
}
}
System.out.println "RETURNING FROM INVOKE METHOD FOR NAME ${name}"
System.out.println("time after ${name} called: ${new Date()}\n")
}
OK SOMETHING IS WRONG ABOVE I DONT KNOW WHAT CAN ANYONE PLEASE HELP ??
The code for invokeMethod and the service don't seem to be the same unless there's a separate FacebookGraphService. Assuming that's the case, then the resp is being caught in the part of your invokeMethod that's inside the if (calledMethod) { block, but since it's not the last line of the method it's not being returned from the call to invokeMethod and therefore is being gobbled up.
Try adding a return to calledMethod.invoke(this, args):
if(calledMethod){
System.out.println("IN IF AND INVOKING METHOD ${calledMethod}")
return calledMethod.invoke(this, args)
}
I have what I think is a simple problem but have been unable to solve...
For some reason I have a controller that uses removeFrom*.save() which throws no errors but does not do anything.
Running
Grails 1.2
Linux/Ubuntu
The following application is stripped down to reproduce the problem...
I have two domain objects via create-domain-class
- Job (which has many notes)
- Note (which belongs to Job)
I have 3 controllers via create-controller
- JobController (running scaffold)
- NoteController (running scaffold)
- JSONNoteController
JSONNoteController has one primary method deleteItem which aims to remove/delete a note.
It does the following
some request validation
removes the note from the job - jobInstance.removeFromNotes(noteInstance).save()
deletes the note - noteInstance.delete()
return a status and remaining data set as a json response.
When I run this request - I get no errors but it appears that jobInstance.removeFromNotes(noteInstance).save() does nothing and does not throw any exception etc.
How can I track down why??
I've attached a sample application that adds some data via BootStrap.groovy.
Just run it - you can view the data via the default scaffold views.
If you run linux, from a command line you can run the following
GET "http://localhost:8080/gespm/JSONNote/deleteItem?job.id=1¬e.id=2"
You can run it over and over again and nothing different happens. You could also paste the URL into your webbrowser if you're running windows.
Please help - I'm stuck!!!
Code is here link text
Note Domain
package beachit
class Note
{
Date dateCreated
Date lastUpdated
String note
static belongsTo = Job
static constraints =
{
}
String toString()
{
return note
}
}
Job Domain
package beachit
class Job
{
Date dateCreated
Date lastUpdated
Date createDate
Date startDate
Date completionDate
List notes
static hasMany = [notes : Note]
static constraints =
{
}
String toString()
{
return createDate.toString() + " " + startDate.toString();
}
}
JSONNoteController
package beachit
import grails.converters.*
import java.text.*
class JSONNoteController
{
def test = { render "foobar test" }
def index = { redirect(action:listAll,params:params) }
// the delete, save and update actions only accept POST requests
//static allowedMethods = [delete:'POST', save:'POST', update:'POST']
def getListService =
{
def message
def status
def all = Note.list()
return all
}
def getListByJobService(jobId)
{
def message
def status
def jobInstance = Job.get(jobId)
def all
if(jobInstance)
{
all = jobInstance.notes
}
else
{
log.debug("getListByJobService job not found for jobId " + jobId)
}
return all
}
def listAll =
{
def message
def status
def listView
listView = getListService()
message = "Done"
status = 0
def response = ['message': message, 'status':status, 'list': listView]
render response as JSON
}
def deleteItem =
{
def jobInstance
def noteInstance
def message
def status
def jobId = 0
def noteId = 0
def instance
def listView
def response
try
{
jobId = Integer.parseInt(params.job?.id)
}
catch (NumberFormatException ex)
{
log.debug("deleteItem error in jobId " + params.job?.id)
log.debug(ex.getMessage())
}
if (jobId && jobId > 0 )
{
jobInstance = Job.get(jobId)
if(jobInstance)
{
if (jobInstance.notes)
{
try
{
noteId = Integer.parseInt(params.note?.id)
}
catch (NumberFormatException ex)
{
log.debug("deleteItem error in noteId " + params.note?.id)
log.debug(ex.getMessage())
}
log.debug("note id =" + params.note.id)
if (noteId && noteId > 0 )
{
noteInstance = Note.get(noteId)
if (noteInstance)
{
try
{
jobInstance.removeFromNotes(noteInstance).save()
noteInstance.delete()
message = "note ${noteId} deleted"
status = 0
}
catch(org.springframework.dao.DataIntegrityViolationException e)
{
message = "Note ${noteId} could not be deleted - references to it exist"
status = 1
}
/*
catch(Exception e)
{
message = "Some New Error!!!"
status = 10
}
*/
}
else
{
message = "Note not found with id ${noteId}"
status = 2
}
}
else
{
message = "Couldn't recognise Note id : ${params.note?.id}"
status = 3
}
}
else
{
message = "No Notes found for Job : ${jobId}"
status = 4
}
}
else
{
message = "Job not found with id ${jobId}"
status = 5
}
listView = getListByJobService(jobId)
} // if (jobId)
else
{
message = "Couldn't recognise Job id : ${params.job?.id}"
status = 6
}
response = ['message': message, 'status':status, 'list' : listView]
render response as JSON
} // deleteNote
}
I got it working... though I cannot explain why.
I replaced the following line in deleteItem
noteInstance = Note.get(noteId)
with the following
noteInstance = jobInstance.notes.find { it.id == noteId }
For some reason the jobInstance.removeFromNotes works with the object returned by that method instead of .get
What makes it stranger is that all other gorm functions (not sure about the dynamic ones actually) work against the noteInstance.get(noteId) method.
At least it's working though!!
See this thread: http://grails.1312388.n4.nabble.com/GORM-doesn-t-inject-hashCode-and-equals-td1370512.html
I would recommend using a base class for your domain objects like this:
abstract class BaseDomain {
#Override
boolean equals(o) {
if(this.is(o)) return true
if(o == null) return false
// hibernate creates dynamic subclasses, so
// checking o.class == class would fail most of the time
if(!o.getClass().isAssignableFrom(getClass()) &&
!getClass().isAssignableFrom(o.getClass())) return false
if(ident() != null) {
ident() == o.ident()
} else {
false
}
}
#Override
int hashCode() {
ident()?.hashCode() ?: 0
}
}
That way, any two objects with the same non-null database id will be considered equal.
I just had this same issue come up. The removeFrom function succeeded, the save succeeded but the physical record in the database wasn't deleted. Here's what worked for me:
class BasicProfile {
static hasMany = [
post:Post
]
}
class Post {
static belongsTo = [basicProfile:BasicProfile]
}
class BasicProfileController {
...
def someFunction
...
BasicProfile profile = BasicProfile.findByUser(user)
Post post = profile.post?.find{it.postType == command.postType && it.postStatus == command.postStatus}
if (post) {
profile.removeFromPost(post)
post.delete()
}
profile.save()
}
So it was the combination of the removeFrom, followed by a delete on the associated domain, and then a save on the domain object.