protected void info(String message) { Log.info(Log.DBMSPOOL, message); }
private void execute(HttpServletRequest req, HttpServletResponse res) throws IOException { String ip = req.getRemoteAddr(); // if we do have the optional x-forwarded-for request header then // use whatever is in it to record ip address of client String forwardedFor = req.getHeader("x-forwarded-for"); if (forwardedFor != null) ip = forwardedFor; Log.info(Log.REQUEST, "=========================================================="); Log.info(Log.REQUEST, "HTML Request (from " + ip + ") : " + req.getRequestURI()); Log.debug(Log.REQUEST, "Method : " + req.getMethod()); Log.debug(Log.REQUEST, "Content type : " + req.getContentType()); // Log.debug(Log.REQUEST, "Context path : "+ req.getContextPath()); // Log.debug(Log.REQUEST, "Char encoding: "+ req.getCharacterEncoding()); Log.debug(Log.REQUEST, "Accept : " + req.getHeader("Accept")); // Log.debug(Log.REQUEST, "Server name : "+ req.getServerName()); // Log.debug(Log.REQUEST, "Server port : "+ req.getServerPort()); // for (Enumeration e = req.getHeaderNames(); e.hasMoreElements();) { // String theHeader = (String)e.nextElement(); // Log.debug(Log.REQUEST, "Got header: "+theHeader); // Log.debug(Log.REQUEST, "With value: "+req.getHeader(theHeader)); // } HttpSession httpSession = req.getSession(); Log.debug(Log.REQUEST, "Session id is " + httpSession.getId()); UserSession session = (UserSession) httpSession.getAttribute("session"); // ------------------------------------------------------------------------ // --- create a new session if doesn't exist if (session == null) { // --- create session session = new UserSession(); httpSession.setAttribute("session", session); Log.debug(Log.REQUEST, "Session created for client : " + ip); } // ------------------------------------------------------------------------ // --- build service request ServiceRequest srvReq = null; // --- create request try { srvReq = ServiceRequestFactory.create(req, res, jeeves.getUploadDir(), jeeves.getMaxUploadSize()); } catch (FileUploadTooBigEx e) { StringBuffer sb = new StringBuffer(); sb.append("File upload too big - exceeds " + jeeves.getMaxUploadSize() + " Mb\n"); sb.append("Error : " + e.getClass().getName() + "\n"); res.sendError(400, sb.toString()); // now stick the stack trace on the end and log the whole lot sb.append("Stack :\n"); sb.append(Util.getStackTrace(e)); Log.error(Log.REQUEST, sb.toString()); return; } catch (Exception e) { StringBuffer sb = new StringBuffer(); sb.append("Cannot build ServiceRequest\n"); sb.append("Cause : " + e.getMessage() + "\n"); sb.append("Error : " + e.getClass().getName() + "\n"); res.sendError(400, sb.toString()); // now stick the stack trace on the end and log the whole lot sb.append("Stack :\n"); sb.append(Util.getStackTrace(e)); Log.error(Log.REQUEST, sb.toString()); return; } // --- execute request jeeves.dispatch(srvReq, session); }
/** * @param request * @param xml * @param context * @param toIndex * @return * @throws Exception */ private int updateTransaction( Element request, Element xml, ServiceContext context, Set<String> toIndex) throws Exception { GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME); DataManager dataMan = gc.getDataManager(); if (context.getUserSession().getUserId() == null) throw new NoApplicableCodeEx("User not authenticated."); int totalUpdated = 0; // Update full metadata if (xml != null) { // Retrieve schema and the related Namespaces String schemaId = gc.getSchemamanager().autodetectSchema(xml); if (schemaId == null) { throw new NoApplicableCodeEx("Can't identify metadata schema"); } // Retrieve the metadata identifier Dbms dbms = (Dbms) context.getResourceManager().open(Geonet.Res.MAIN_DB); String uuid = gc.getDataManager().extractUUID(schemaId, xml); if (uuid.length() == 0) { throw new NoApplicableCodeEx("Metadata identifier not provided"); } // Update metadata record String id = dataMan.getMetadataId(dbms, uuid); if (id == null) return totalUpdated; if (!gc.getAccessManager().canEdit(context, id)) throw new NoApplicableCodeEx("User not allowed to update this metadata(" + id + ")."); String changeDate = null; boolean validate = false; boolean ufo = false; boolean index = false; String language = context.getLanguage(); dataMan.updateMetadata( context, dbms, id, xml, validate, ufo, index, language, changeDate, false); dbms.commit(); toIndex.add(id); totalUpdated++; return totalUpdated; // Update properties } else { // first, search the record in the database to get the record id Element constr = (Element) request.getChild("Constraint", Csw.NAMESPACE_CSW).clone(); List<Element> results = getResultsFromConstraints(context, constr); List<Element> recordProperties = (List<Element>) request.getChildren("RecordProperty", Csw.NAMESPACE_CSW); Iterator<Element> it = results.iterator(); if (!it.hasNext()) return totalUpdated; Dbms dbms = (Dbms) context.getResourceManager().open(Geonet.Res.MAIN_DB); Set updatedMd = new HashSet<String>(); // Process all records selected while (it.hasNext()) { Element result = it.next(); String uuid = result.getChildText("identifier", Csw.NAMESPACE_DC); String id = dataMan.getMetadataId(dbms, uuid); String changeDate = null; if (id == null) continue; if (!dataMan.getAccessManager().canEdit(context, id)) throw new NoApplicableCodeEx("User not allowed to update this metadata(" + id + ")."); Element metadata = dataMan.getMetadata(context, id, false, false, true); metadata.removeChild("info", Edit.NAMESPACE); // Retrieve the schema and Namespaces of metadata to update String schemaId = gc.getDataManager().autodetectSchema(metadata); if (schemaId == null) { throw new NoApplicableCodeEx("Can't identify metadata schema"); } Map mapNs = retrieveNamepacesForSchema(gc.getDataManager().getSchema(schemaId)); boolean metadataChanged = false; // Process properties to update for (Element recordProperty : recordProperties) { Element propertyNameEl = recordProperty.getChild("Name", Csw.NAMESPACE_CSW); Element propertyValueEl = recordProperty.getChild("Value", Csw.NAMESPACE_CSW); String propertyName = propertyNameEl.getText(); String propertyValue = propertyValueEl.getText(); // Get XPath for queriable name, i provided in propertyName. // Otherwise assume propertyName contains full XPath to property to update String xpathProperty = FieldMapper.mapXPath(propertyName, schemaId); if (xpathProperty == null) { xpathProperty = propertyName; } Log.info(Geonet.CSW, "Xpath of property: " + xpathProperty); XPath xpath = new JDOMXPath(xpathProperty); xpath.setNamespaceContext(new SimpleNamespaceContext(mapNs)); Object propEl = xpath.selectSingleNode(metadata); Log.info(Geonet.CSW, "XPath found in metadata: " + (propEl != null)); // If a property is not found in metadata, just ignore it. if (propEl != null) { if (propEl instanceof Element) { ((Element) propEl).setText(propertyValue); metadataChanged = true; } else if (propEl instanceof Attribute) { ((Attribute) propEl).setValue(propertyValue); metadataChanged = true; } } } // for(Element recordProperty : recordProperties) // Update the metadata with changes if (metadataChanged) { boolean validate = false; boolean ufo = false; boolean index = false; String language = context.getLanguage(); dataMan.updateMetadata( context, dbms, id, metadata, validate, ufo, index, language, changeDate, false); updatedMd.add(id); totalUpdated++; } } dbms.commit(); toIndex.addAll(updatedMd); return totalUpdated; } }