-
-
Notifications
You must be signed in to change notification settings - Fork 487
/
Show.java
180 lines (149 loc) · 7.02 KB
/
Show.java
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
//=============================================================================
//=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
//=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
//=== and United Nations Environment Programme (UNEP)
//===
//=== This program is free software; you can redistribute it and/or modify
//=== it under the terms of the GNU General Public License as published by
//=== the Free Software Foundation; either version 2 of the License, or (at
//=== your option) any later version.
//===
//=== This program is distributed in the hope that it will be useful, but
//=== WITHOUT ANY WARRANTY; without even the implied warranty of
//=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//=== General Public License for more details.
//===
//=== You should have received a copy of the GNU General Public License
//=== along with this program; if not, write to the Free Software
//=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
//===
//=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
//=== Rome - Italy. email: geonetwork@osgeo.org
//==============================================================================
package org.fao.geonet.services.metadata;
import java.nio.file.Path;
import org.fao.geonet.GeonetContext;
import org.fao.geonet.Util;
import org.fao.geonet.constants.Geonet;
import org.fao.geonet.constants.Params;
import org.fao.geonet.domain.AbstractMetadata;
import org.fao.geonet.domain.ReservedOperation;
import org.fao.geonet.exceptions.MetadataNotFoundEx;
import org.fao.geonet.kernel.DataManager;
import org.fao.geonet.kernel.SchemaManager;
import org.fao.geonet.kernel.XmlSerializer;
import org.fao.geonet.kernel.datamanager.IMetadataUtils;
import org.fao.geonet.lib.Lib;
import org.fao.geonet.services.Utils;
import org.fao.geonet.utils.Xml;
import org.jdom.Attribute;
import org.jdom.Element;
import jeeves.server.ServiceConfig;
import jeeves.server.UserSession;
import jeeves.server.context.ServiceContext;
//=============================================================================
/**
* Retrieves a particular metadata. Access is restricted
*/
@Deprecated
public class Show extends ShowViewBaseService {
//--------------------------------------------------------------------------
//---
//--- Init
//---
//--------------------------------------------------------------------------
private final static String KEY = "SHOW_METADATA";
//--------------------------------------------------------------------------
//---
//--- Service
//---
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//---
//--- Variables
//---
//--------------------------------------------------------------------------
private boolean cache;
private static void cache(UserSession userSession, Element elMd, String id) {
userSession.setProperty(KEY + id, elMd);
}
public static Element getCached(UserSession userSession, String id) {
return (Element) userSession.getProperty(KEY + id);
}
static void unCache(UserSession userSession, String id) {
userSession.removeProperty(KEY + id);
}
public void init(Path appPath, ServiceConfig params) throws Exception {
super.init(appPath, params);
cache = "y".equalsIgnoreCase(params.getValue("cache", "n"));
}
public Element exec(Element params, ServiceContext context) throws Exception {
UserSession session = context.getUserSession();
//-----------------------------------------------------------------------
//--- handle current tab
Element elCurrTab = params.getChild(Params.CURRTAB);
boolean removeSchemaLocation = Util.getParam(params, "removeSchemaLocation", "false").equals("true");
if (elCurrTab != null)
session.setProperty(Geonet.Session.METADATA_SHOW, elCurrTab.getText());
//-----------------------------------------------------------------------
//--- check access
GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
DataManager dm = gc.getBean(DataManager.class);
SchemaManager sm = gc.getBean(SchemaManager.class);
String id = Utils.getIdentifierFromParameters(params, context);
boolean skipPopularity = this.skipPopularity;
if (!skipPopularity) { // skipPopularity could be a URL param as well
String skip = Util.getParam(params, "skipPopularity", "n");
skipPopularity = skip.equals("y");
}
boolean witholdWithheldElements = Util.getParam(params, "hide_withheld", false);
if (XmlSerializer.getThreadLocal(false) != null || witholdWithheldElements) {
XmlSerializer.getThreadLocal(true).setForceFilterEditOperation(witholdWithheldElements);
}
if (id == null) {
throw new MetadataNotFoundEx("Metadata not found.");
}
Lib.resource.checkPrivilege(context, id, ReservedOperation.view);
//-----------------------------------------------------------------------
//--- get metadata
Element elMd;
boolean addEditing = false;
if (!skipInfo) {
boolean withValidationErrors = false, keepXlinkAttributes = false;
elMd = gc.getBean(DataManager.class).getMetadata(context, id, addEditing, withValidationErrors, keepXlinkAttributes);
} else {
elMd = dm.getMetadataNoInfo(context, id);
}
if (elMd == null) {
throw new MetadataNotFoundEx(id);
}
if (addRefs) { // metadata.show for GeoNetwork needs geonet:element
elMd = dm.enumerateTree(elMd);
}
//
// setting schemaLocation - if there isn't one then use the schemaLocation
// that is in the GeoNetwork schema identification and if there isn't one
// of those then build one pointing to the XSD in GeoNetwork
AbstractMetadata info = context.getBean(IMetadataUtils.class).findOne(id);
Attribute schemaLocAtt = sm.getSchemaLocation(info.getDataInfo().getSchemaId(), context);
if (schemaLocAtt != null) {
if (elMd.getAttribute(schemaLocAtt.getName(), schemaLocAtt.getNamespace()) == null) {
elMd.setAttribute(schemaLocAtt);
// make sure namespace declaration for schemalocation is present -
// remove it first (does nothing if not there) then add it
elMd.removeNamespaceDeclaration(schemaLocAtt.getNamespace());
elMd.addNamespaceDeclaration(schemaLocAtt.getNamespace());
}
}
//--- increase metadata popularity
if (!skipPopularity)
dm.increasePopularity(context, id);
if (cache) {
cache(context.getUserSession(), elMd, id);
}
if (removeSchemaLocation) {
elMd.removeAttribute("schemaLocation", Xml.xsiNS);
}
return elMd;
}
}