summaryrefslogtreecommitdiffstats
path: root/idrop-web/grails-app/controllers/org/irods/mydrop/controller/HomeController.groovy
blob: e5f03371ebd69b3e6b3c35f4e6f774412cebaf88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package org.irods.mydrop.controller

import org.irods.jargon.core.connection.IRODSAccount
import org.irods.jargon.core.exception.JargonException
import org.irods.jargon.core.exception.OperationNotSupportedByThisServerException
import org.irods.jargon.core.exception.SpecificQueryException
import org.irods.jargon.core.pub.IRODSAccessObjectFactory
import org.irods.jargon.core.utils.MiscIRODSUtils
import org.irods.mydrop.service.SharingService
import org.irods.mydrop.service.StarringService

class HomeController {

	StarringService starringService
	SharingService sharingService
	IRODSAccessObjectFactory irodsAccessObjectFactory

	/**
	 * Interceptor grabs IRODSAccount from the SecurityContextHolder
	 */
	def beforeInterceptor = [action:this.&auth, except:'link']
	IRODSAccount irodsAccount

	def auth() {
		if(!session["SPRING_SECURITY_CONTEXT"]) {
			redirect(controller:"login", action:"login")
			return false
		}
		irodsAccount = session["SPRING_SECURITY_CONTEXT"]
	}

	def afterInterceptor = {
		log.debug("closing the session")
		irodsAccessObjectFactory.closeSession()
	}

	def index() {
		log.info("index")
		boolean shareSupported = sharingService.isSharingSupported(irodsAccount)
		render(view:"index", model:[shareSupported:shareSupported])
	}

	/**
	 * Link action will use an anonymous login to pre-set the view to a desired path.  This bypasss the filter
	 * 
	 * @return
	 */
	def link() {
		log.info "link()"
		def irodsURIString = params['irodsURI']
		if (irodsURIString == null) {
			def message = message(code:"error.no.uri.provided")
			response.sendError(500,message)
			return
		}

		log.info("irodsURI: ${irodsURIString}")

		// TODO: refactor, add check to see if on same grid, in which case the account should not be changed
		String mungedIRODSURI = irodsURIString.replaceAll(" ", "&&space&&")
		URI irodsURI = new URI(mungedIRODSURI)
		String filePath = irodsURI.getPath()
		log.info("irodsFilePath:${filePath}")
		filePath = filePath.replaceAll("&&space&&", " ")
		log.info("irodsFilePath:${filePath}")
		String zone = MiscIRODSUtils.getZoneInPath(filePath)
		log.info("zone:${zone}")

		irodsAccount = session["SPRING_SECURITY_CONTEXT"]
		if (irodsAccount == null) {
			log.info("no account set up, create an anonymous login")
			irodsAccount = anonymousIrodsAccountForURIString(mungedIRODSURI)
			session["SPRING_SECURITY_CONTEXT"] = irodsAccount
		}

		/*
		 * Need to figure out how to signal interface to 'reset' to new account and path?  
		 */

		render(view:"link", model:[absPath:filePath])
	}

	def starredCollections() {
		log.info "starredCollections()"

		try {
			def listing = starringService.listStarredCollections(irodsAccount, 0)

			if (listing.isEmpty()) {
				render(view:"noInfo")
			} else {
				render(view:"quickViewList",model:[listing:listing])
			}
		} catch (SpecificQueryException sqe) {
			log.error("error in specific query", sqe)
			render(view:"noSupport")
		} catch (JargonException je) {
			log.error("jargon exception", je)
			response.sendError(500,je.message)
		}
	}

	def starredDataObjects() {
		log.info "starredDataObjects()"
		try {
			def listing = starringService.listStarredDataObjects(irodsAccount, 0)
			if (listing.isEmpty()) {
				render(view:"noInfo")
			} else {
				render(view:"quickViewList",model:[listing:listing])
			}
		} catch (SpecificQueryException sqe) {
			log.error("error in specific query", sqe)
			render(view:"noSupport")
		} catch (JargonException je) {
			log.error("jargon exception", je)
			response.sendError(500,je.message)
		}
	}

	/**
	 * Listing of collections shared by me with others
	 * @return
	 */
	def sharedCollectionsByMe() {
		log.info "sharedCollectionsByMe"

		boolean sharing = sharingService.isSharingSupported(irodsAccount)
		if (!sharing) {
			log.info("no sharing support on this grid")
			render(view:"noSupport")
			return
		}


		try {
			def listing = sharingService.listCollectionsSharedByMe(irodsAccount);
			if (listing.isEmpty()) {
				render(view:"noInfo")
			} else {
				render(view:"shareQuickViewList",model:[listing:listing])
			}
		} catch (SpecificQueryException sqe) {
			log.error("error in specific query", sqe)
			render(view:"noSupport")
		} catch (OperationNotSupportedByThisServerException sqe) {
			log.error("error in specific query", sqe)
			render(view:"noSupport")
		} catch (JargonException je) {
			log.error("jargon exception", je)
			response.sendError(500,je.message)
		}
	}

	/**
	 * Listing of collections shared by me with others
	 * @return
	 */
	def sharedCollectionsWithMe() {
		log.info "sharedCollectionsByMe"

		boolean sharing = sharingService.isSharingSupported(irodsAccount)
		if (!sharing) {
			log.info("no sharing support on this grid")
			render(view:"noSupport")
			return
		}

		try {
			def listing = sharingService.listCollectionsSharedWithMe(irodsAccount)
			if (listing.isEmpty()) {
				render(view:"noInfo")
			} else {
				render(view:"shareWithMeQuickViewList",model:[listing:listing])
			}
		} catch (SpecificQueryException sqe) {
			log.error("error in specific query", sqe)
			render(view:"noSupport")
		} catch (OperationNotSupportedByThisServerException sqe) {
			log.error("error in specific query", sqe)
			render(view:"noSupport")
		} catch (JargonException je) {
			log.error("jargon exception", je)
			response.sendError(500,je.message)
		}
	}


	// FIXME: refactor into jargon-core
	private IRODSAccount anonymousIrodsAccountForURIString(String uriString) {
		// get an anonymous account based on the provided URI
		URI irodsURI = new URI(uriString)
		String filePath = irodsURI.getPath()
		log.info("irodsFilePath:${filePath}")
		String zone = MiscIRODSUtils.getZoneInPath(filePath)
		log.info("zone:${zone}")
		IRODSAccount irodsAccount = IRODSAccount.instanceForAnonymous(irodsURI.getHost(),
				irodsURI.getPort(), "", zone,
				"")
		return irodsAccount
	}
}