/** * Find the current dasextension::query::sites element or create a new one in the proper place in * the given dasextension element. * * @param dasExt - mandatory - must be a valid dasextension Element * @return */ public ExtensibleElement getSites(ExtensibleElement dasExt) { // get reference to the dasextension::query element in feed ExtensibleElement query = this.getQuery(dasExt); // get reference to the dasextension::query::sites element in feed QName sitesQname = new QName(this.dasextensionNamespace, "sites"); ExtensibleElement sites = query.getExtension(sitesQname); if (NullChecker.isEmpty(sites)) { ExtensibleElement queryFirstChild = query.getFirstChild(); if (NullChecker.isEmpty(queryFirstChild)) { // no children in query, so merely add sites element sites = query.addExtension(sitesQname); } else { QName fCQname = queryFirstChild.getQName(); if ("parameters".equals(fCQname.getLocalPart())) { ExtensibleElement querySecondChild = query.getNextSibling(); if (NullChecker.isEmpty(querySecondChild)) { // no second child in query, so add sites after parameters sites = query.addExtension(sitesQname); } else { // add sites after parameters, and before the second child QName sCQname = querySecondChild.getQName(); sites = query.addExtension(sitesQname, sCQname); } } else { // another element besides parameters is first child, // so add sites before this sites = dasExt.addExtension(sitesQname, fCQname); } } } return sites; }
/** * Add an new empty feed::dasextension element with only a completed management element to the * given Feed. Sets the responseTime value to now. Note: A proper sites::site element must be * added to the new feed::dasextension element for this new feed::dasextension element to be * valid. * * @param feed - mandatory * @return */ private Feed addEmptyDasExtension(Feed feed) { // check to make sure there's a dasextension QName dasextensionQname = new QName(this.dasextensionNamespace, "dasextension"); ExtensibleElement dasExt = feed.getExtension(dasextensionQname); if (NullChecker.isEmpty(dasExt)) { dasExt = feed.addExtension(dasextensionQname); // add the dasextension::management element QName managementQname = new QName(this.dasextensionNamespace, "management"); ExtensibleElement management = dasExt.getExtension(managementQname); if (NullChecker.isEmpty(management)) { management = dasExt.addExtension(managementQname); } QName responseTimeQname = new QName(this.dasextensionNamespace, "responseTime"); ExtensibleElement responseTime = management.getExtension(responseTimeQname); if (NullChecker.isEmpty(responseTime)) { responseTime = management.addExtension(responseTimeQname); } // set/reset the response time to now XMLGregorianCalendar xmlGregCalResponseTime = GregorianDateUtil.getGregorianCalendarByDate(new Date()); responseTime.setText(xmlGregCalResponseTime.toString()); // TODO: Verify date format } return feed; }
/** * Find the current dasextension::query element or create a new one in the proper place in the * given dasextension element. * * @param dasExt - mandatory - must be a valid dasextension Element * @return */ private ExtensibleElement getQuery(ExtensibleElement dasExt) { QName queryQname = new QName(this.dasextensionNamespace, "query"); // attempt to get query element ExtensibleElement query = dasExt.getExtension(queryQname); if (NullChecker.isEmpty(query)) { // there is no query element, so create one in the proper place ExtensibleElement firstChild = dasExt.getFirstChild(); if (NullChecker.isEmpty(firstChild)) { // no children, so merely add query query = dasExt.addExtension(queryQname); } else { QName fCQname = firstChild.getQName(); if ("path".equals(fCQname.getLocalPart())) { // path is the firstChild, so get the second child ExtensibleElement secondChild = firstChild.getNextSibling(); if (NullChecker.isEmpty(secondChild)) { // no second child, so add query after path query = dasExt.addExtension(queryQname); } else { QName sCQname = secondChild.getQName(); // have a second child, so add query after // path element and before second child query = dasExt.addExtension(queryQname, sCQname); } } else { // add query before the first child element query = dasExt.addExtension(queryQname, fCQname); } } } return query; }
/** * Adds a single error element into the DAS dasextension Atom Feed, i.e. adds one of the following * inside of feed::dasextension::query::errors: <error> <errorSeverity></errorSeverity> * <errorCode></errorCode> <errorValue></errorValue> <errorLocation></errorLocation> </error> * Note: The feed parameter must contain a constructed DAS dasextension Atom Feed. * * @param feed - mandatory - must contain a valid feed::dasextension Element * @param errorSeverity - optional * @param errorCode - optional * @param errorValue - optional * @param errorLocation - optional * @return */ public Feed addError( Feed feed, final String errorSeverity, final String errorCode, final String errorValue, final String errorLocation) { if (NullChecker.isNotEmpty(feed)) { // get reference to the dasextension element in feed ExtensibleElement dasExt = feed.getExtension(new QName(this.dasextensionNamespace, "dasextension")); if (NullChecker.isNotEmpty(dasExt)) { // get reference to the dasextension::query::errors element in feed // -- if errors doesn't exist, this method will add it ExtensibleElement errors = this.getErrors(dasExt); // add the dasextension::query::errors::error element QName errorQname = new QName(this.dasextensionNamespace, "error"); ExtensibleElement errorsError = errors.addExtension(errorQname); if (NullChecker.isNotEmpty(errorSeverity)) { QName errorSeverityQname = new QName(this.dasextensionNamespace, "errorSeverity"); ExtensibleElement errorSeverityElement = errorsError.addExtension(errorSeverityQname); errorSeverityElement.setText(errorSeverity); } if (NullChecker.isNotEmpty(errorCode)) { QName errorCodeQname = new QName(this.dasextensionNamespace, "errorCode"); ExtensibleElement errorCodeElement = errorsError.addExtension(errorCodeQname); errorCodeElement.setText(errorCode); } if (NullChecker.isNotEmpty(errorValue)) { QName errorValueQname = new QName(this.dasextensionNamespace, "errorValue"); ExtensibleElement errorValueElement = errorsError.addExtension(errorValueQname); errorValueElement.setText(errorValue); } if (NullChecker.isNotEmpty(errorLocation)) { QName errorLocationQname = new QName(this.dasextensionNamespace, "errorLocation"); ExtensibleElement errorLocationElement = errorsError.addExtension(errorLocationQname); errorLocationElement.setText(errorLocation); } } } return feed; }
/** * Find the current dasextension::query::errors element or create a new one in the proper place in * the given dasextension element. * * @param dasExt - mandatory - must be a valid dasextension Element * @return */ public ExtensibleElement getErrors(ExtensibleElement dasExt) { // get reference to the dasextension::query element in feed ExtensibleElement query = this.getQuery(dasExt); // get reference to the dasextension::query::errors element in feed QName errorsQname = new QName(this.dasextensionNamespace, "errors"); ExtensibleElement errors = query.getExtension(errorsQname); if (NullChecker.isEmpty(errors)) { List<Element> queryChildren = query.getElements(); if (NullChecker.isEmpty(queryChildren)) { // no children in query, so merely add errors element errors = query.addExtension(errorsQname); } else { // add errors after the last child errors = query.addExtension(errorsQname); } } return errors; }
/** * Places the org.osehra.integration.http.uri.UriInfo uriInfo instance's query parameter values * into the DAS dasextension Atom Feed at feed::dasextension::query::parameters. Notes: The feed * parameter must contain a constructed DAS dasextension Atom Feed. This method will remove any * existing feed::dasextension::query::parameters::entry elements before adding the new entries * from uriInfo. * * @param feed -mandatory - must contain a valid feed::dasextension Element * @param uriInfo - mandatory * @return */ public Feed addQueryParameters(Feed feed, final javax.ws.rs.core.UriInfo uriInfo) { if (NullChecker.isNotEmpty(feed)) { // get reference to the dasextension element in feed ExtensibleElement dasExt = feed.getExtension(new QName(this.dasextensionNamespace, "dasextension")); if (NullChecker.isNotEmpty(dasExt)) { // check for a UriInfo to input if (NullChecker.isNotEmpty(uriInfo) && !(uriInfo.getQueryParameters().isEmpty())) { // get or create reference to the dasextension::query::parameters element in feed ExtensibleElement parameters = this.getQueryParameters(dasExt); // discard any existing path dasextension::path::parameter::entry elements List<Element> queryParameterEntries = parameters.getElements(); if (NullChecker.isNotEmpty(queryParameterEntries)) { for (Element curQueryParameterEntry : queryParameterEntries) { curQueryParameterEntry.discard(); } } // add the dasextension::query::parameters::entry elements from uriInfo QName entryQname = new QName(this.dasextensionNamespace, "entry"); QName keyQname = new QName(this.dasextensionNamespace, "key"); QName valueQname = new QName(this.dasextensionNamespace, "value"); MultivaluedMap<String, String> queryParametersExt = uriInfo.getQueryParameters(); Set<Map.Entry<String, List<String>>> queryParamsEntries = queryParametersExt.entrySet(); for (Map.Entry<String, List<String>> queryParameterEntry : queryParamsEntries) { ExtensibleElement entry = parameters.addExtension(entryQname); ExtensibleElement key = entry.addExtension(keyQname); key.setText(queryParameterEntry.getKey()); for (String entryValue : queryParameterEntry.getValue()) { ExtensibleElement value = entry.addExtension(valueQname); value.setText(entryValue); } } } } } return feed; }
/** * Find the current dasextension::path element or create a new one in the proper place in the * given dasextension element. * * @param dasExt - must be a valid dasextension Element * @return */ private ExtensibleElement getPath(ExtensibleElement dasExt) { // get or create a reference to the dasextension::path element in feed QName pathQname = new QName(this.dasextensionNamespace, "path"); ExtensibleElement path = null; ExtensibleElement firstChild = (ExtensibleElement) dasExt.getFirstChild(); if (NullChecker.isNotEmpty(firstChild)) { QName fCQname = firstChild.getQName(); if ("path".equals(fCQname.getLocalPart())) { // path is the firstChild path = firstChild; } else { // no path, so add it before firstChild path = dasExt.addExtension(pathQname, fCQname); } } else { // no children, so create a "path" element path = dasExt.addExtension(pathQname); } return path; }
/** * Adds a single error element into the DAS dasextension Atom Feed at * feed::dasextension::query::errors. * * @param feed - mandatory - must contain a valid feed::dasextension Element * @param errorsError - mandatory - must contain a valid dasextension::query::errors::error * Element * @return */ private Feed addError(Feed feed, final ExtensibleElement errorsError) { if (NullChecker.isNotEmpty(feed)) { // get reference to the dasextension element in feed ExtensibleElement dasExt = feed.getExtension(new QName(this.dasextensionNamespace, "dasextension")); if (NullChecker.isNotEmpty(dasExt)) { // get feed::dasextension::query::errors and add the new // error into it in the feed ExtensibleElement errors = this.getErrors(dasExt); errors.addExtension(errorsError); } } return feed; }
/** * Adds a single site element into the given DAS dasextension Atom Feed at * feed::dasextension::query::sites. * * @param feed * @param site - mandatory - must contain a valid dasextension::query::sites::site Element * @return */ private Feed addSite(Feed feed, final ExtensibleElement site) { if (NullChecker.isNotEmpty(feed)) { // get reference to the dasextension element in feed ExtensibleElement dasExt = feed.getExtension(new QName(this.dasextensionNamespace, "dasextension")); if (NullChecker.isNotEmpty(dasExt)) { // get feed::dasextension::query::sites and add the new // site into it in the feed ExtensibleElement sites = this.getSites(dasExt); sites.addExtension(site); } } return feed; }
/** * Find the current dasextension::query::parameters element or create a new one in the proper * place in the given dasextension element. * * @param dasExt - mandatory - must be a valid dasextension Element * @return */ private ExtensibleElement getQueryParameters(ExtensibleElement dasExt) { QName parametersQname = new QName(this.dasextensionNamespace, "parameters"); ExtensibleElement parameters = null; ExtensibleElement query = this.getQuery(dasExt); ExtensibleElement queryFirstChild = query.getFirstChild(); if (NullChecker.isEmpty(queryFirstChild)) { // no children in query, so just query::parameters parameters = query.addExtension(parametersQname); } else { QName fCQname = queryFirstChild.getQName(); if ("parameters".equals(fCQname.getLocalPart())) { // already have a query::parameters parameters = queryFirstChild; } else { // firstQueryChild is not parameters, so add query::parameters before it parameters = query.addExtension(parametersQname, fCQname); } } return parameters; }
/** * Find the current dasextension::path::parameters element or create a new one in the proper place * in the given dasextension element. * * @param dasExt - mandatory - must be a valid dasextension Element * @return */ private ExtensibleElement getPathParameters(ExtensibleElement dasExt) { QName parametersQname = new QName(this.dasextensionNamespace, "parameters"); ExtensibleElement parameters = null; ExtensibleElement path = this.getPath(dasExt); ExtensibleElement pathFirstChild = path.getFirstChild(); if (NullChecker.isNotEmpty(pathFirstChild)) { // already have a parameters parameters = pathFirstChild; } else { // no parameters, so add one parameters = path.addExtension(parametersQname); } return parameters; }
/** * Adds the given dasextension::query::parameters Element into the given feed. Note: This method * will merge the given queryParameters with any existing query::parameters in the given feed. * * @param feed - mandatory - must contain a valid feed::dasextension Element * @param queryParameters - mandatory - must contain a valid and non-empty * dasextension::query::parameters Element. * @return */ private Feed addQueryParameters(Feed feed, final ExtensibleElement queryParameters) { if (NullChecker.isNotEmpty(feed)) { if (NullChecker.isNotEmpty(queryParameters)) { // get reference to the dasextension element in feed ExtensibleElement dasExt = feed.getExtension(new QName(this.dasextensionNamespace, "dasextension")); if (NullChecker.isNotEmpty(dasExt)) { List<Element> queryParametersEntries = queryParameters.getElements(); if (NullChecker.isNotEmpty(queryParametersEntries)) { // get or create the new dasextension::path::parameters Element ExtensibleElement queryParametersFeed = this.getQueryParameters(dasExt); // add in the dasextension::path::parameters::entry Elements for (Element curQueryParameterEntry : queryParametersEntries) { queryParametersFeed.addExtension(curQueryParameterEntry); } } } } } return feed; }
/* public static Entry ToPackageEntry(PackageItem p, UriInfo uriInfo) { UriBuilder base; if(p.isHistoricalVersion()) { base = uriInfo.getBaseUriBuilder().path("packages").path(p.getName()).path("versions").path(Long.toString(p.getVersionNumber())); } else { base = uriInfo.getBaseUriBuilder().path("packages").path(p.getName()); } //NOTE: Entry extension is not supported in RESTEasy. We need to either use Abdera or get extension //supported in RESTEasy //PackageMetadata metadata = new PackageMetadata(); //metadata.setUuid(p.getUUID()); //metadata.setCreated(p.getCreatedDate().getTime()); //metadata.setLastModified(p.getLastModified().getTime()); //metadata.setLastContributor(p.getLastContributor()); //c.setJAXBObject(metadata); Entry e =new Entry(); e.setTitle(p.getTitle()); e.setSummary(p.getDescription()); e.setPublished(new Date(p.getLastModified().getTimeInMillis())); e.setBase(base.clone().build()); e.setId(base.clone().build()); Iterator<AssetItem> i = p.getAssets(); while (i.hasNext()) { AssetItem item = i.next(); Link link = new Link(); link.setHref((base.clone().path("assets").path(item.getName())).build()); link.setTitle(item.getTitle()); link.setRel("asset"); e.getLinks().add(link); } Content c = new Content(); c.setType(MediaType.APPLICATION_OCTET_STREAM_TYPE); c.setSrc(base.clone().path("binary").build()); e.setContent(c); return e; }*/ public static Entry toAssetEntryAbdera(AssetItem a, UriInfo uriInfo) { URI baseURL; if (a.isHistoricalVersion()) { baseURL = uriInfo .getBaseUriBuilder() .path("packages/{packageName}/assets/{assetName}/versions/{version}") .build(a.getModuleName(), a.getName(), Long.toString(a.getVersionNumber())); } else { baseURL = uriInfo .getBaseUriBuilder() .path("packages/{packageName}/assets/{assetName}") .build(a.getModuleName(), a.getName()); } Factory factory = Abdera.getNewFactory(); org.apache.abdera.model.Entry e = factory.getAbdera().newEntry(); e.setTitle(a.getTitle()); e.setSummary(a.getDescription()); e.setPublished(new Date(a.getLastModified().getTimeInMillis())); e.setBaseUri(baseURL.toString()); e.addAuthor(a.getLastContributor()); e.setId(baseURL.toString()); // generate meta data ExtensibleElement extension = e.addExtension(METADATA); ExtensibleElement childExtension = extension.addExtension(ARCHIVED); // childExtension.setAttributeValue("type", ArtifactsRepository.METADATA_TYPE_STRING); childExtension.addSimpleExtension(VALUE, a.isArchived() ? "true" : "false"); childExtension = extension.addExtension(UUID); childExtension.addSimpleExtension(VALUE, a.getUUID()); childExtension = extension.addExtension(STATE); childExtension.addSimpleExtension(VALUE, a.getState() == null ? "" : a.getState().getName()); childExtension = extension.addExtension(FORMAT); childExtension.addSimpleExtension(VALUE, a.getFormat()); childExtension = extension.addExtension(VERSION_NUMBER); childExtension.addSimpleExtension(VALUE, String.valueOf(a.getVersionNumber())); childExtension = extension.addExtension(CHECKIN_COMMENT); childExtension.addSimpleExtension(VALUE, a.getCheckinComment()); List<CategoryItem> categories = a.getCategories(); childExtension = extension.addExtension(CATEGORIES); for (CategoryItem c : categories) { childExtension.addSimpleExtension(VALUE, c.getName()); } org.apache.abdera.model.Content content = factory.newContent(); content.setSrc(UriBuilder.fromUri(baseURL).path("binary").build().toString()); content.setMimeType("application/octet-stream"); content.setContentType(Type.MEDIA); e.setContentElement(content); return e; }
public static Entry toPackageEntryAbdera(ModuleItem p, UriInfo uriInfo) { URI baseURL; if (p.isHistoricalVersion()) { baseURL = uriInfo .getBaseUriBuilder() .path("packages/{packageName}/versions/{version}") .build(p.getName(), Long.toString(p.getVersionNumber())); } else { baseURL = uriInfo.getBaseUriBuilder().path("packages/{packageName}").build(p.getName()); } Factory factory = Abdera.getNewFactory(); org.apache.abdera.model.Entry e = factory.getAbdera().newEntry(); e.setTitle(p.getTitle()); e.setSummary(p.getDescription()); e.setPublished(new Date(p.getLastModified().getTimeInMillis())); e.setBaseUri(baseURL.toString()); e.addAuthor(p.getLastContributor()); e.setId(baseURL.toString()); Iterator<AssetItem> i = p.getAssets(); while (i.hasNext()) { AssetItem item = i.next(); org.apache.abdera.model.Link l = factory.newLink(); l.setHref( UriBuilder.fromUri(baseURL).path("assets/{assetName}").build(item.getName()).toString()); l.setTitle(item.getTitle()); l.setRel("asset"); e.addLink(l); } // generate meta data ExtensibleElement extension = e.addExtension(METADATA); ExtensibleElement childExtension = extension.addExtension(ARCHIVED); // childExtension.setAttributeValue("type", ArtifactsRepository.METADATA_TYPE_STRING); childExtension.addSimpleExtension(VALUE, p.isArchived() ? "true" : "false"); childExtension = extension.addExtension(UUID); childExtension.addSimpleExtension(VALUE, p.getUUID()); childExtension = extension.addExtension(STATE); childExtension.addSimpleExtension(VALUE, p.getState() == null ? "" : p.getState().getName()); childExtension = extension.addExtension(VERSION_NUMBER); childExtension.addSimpleExtension(VALUE, String.valueOf(p.getVersionNumber())); childExtension = extension.addExtension(CHECKIN_COMMENT); childExtension.addSimpleExtension(VALUE, p.getCheckinComment()); org.apache.abdera.model.Content content = factory.newContent(); content.setSrc(UriBuilder.fromUri(baseURL).path("binary").build().toString()); content.setMimeType("application/octet-stream"); content.setContentType(Type.MEDIA); e.setContentElement(content); return e; }
/** * Adds a single site element into the DAS dasextension Atom Feed. i.e. adds one of the following * inside of feed::dasextension::query::sites: <site> <id></id> <name></name> * <expectedCount></expectedCount> <retrievedCount></retrievedCount> <status></status> * <error></error> </site> Notes: The feed parameter must contain a constructed DAS dasextension * Atom Feed. An existing site element with the same site::name value as the given siteName value * will be removed before the new site element is added. * * @param feed - mandatory - must contain a valid feed::dasextension Element * @param siteName - mandatory * @param siteId - optional * @param siteHttpStatus - mandatory * @param expectedCount - optional * @param retrievedCount - optional * @param siteErrorMessage - optional * @return */ public Feed addSite( Feed feed, final String siteName, final String siteId, final int siteHttpStatus, final String expectedCount, final String retrievedCount, final String siteErrorMessage) { if (NullChecker.isNotEmpty(feed)) { // get reference to the dasextension element in feed ExtensibleElement dasExt = feed.getExtension(new QName(this.dasextensionNamespace, "dasextension")); if (NullChecker.isNotEmpty(dasExt)) { // get reference to the dasextension::query::sites element in feed ExtensibleElement sites = this.getSites(dasExt); // search for any existing site children by site::name value with // the new siteName, and if one is found, remove it List<Element> siteElements = sites.getElements(); for (Element curSiteElement : siteElements) { List<Element> siteChildElements = curSiteElement.getElements(); for (Element curSiteChildElement : siteChildElements) { if ("name".equals(curSiteChildElement.getQName().getLocalPart())) { if (siteName.equals(curSiteChildElement.getText())) { curSiteElement.discard(); } } } } // add the dasextension::query::sites::site element QName siteQname = new QName(this.dasextensionNamespace, "site"); ExtensibleElement site = sites.addExtension(siteQname); if (NullChecker.isNotEmpty(siteId)) { QName idQname = new QName(this.dasextensionNamespace, "id"); ExtensibleElement idElement = site.addExtension(idQname); idElement.setText(siteId); } QName nameQname = new QName(this.dasextensionNamespace, "name"); ExtensibleElement nameElement = site.addExtension(nameQname); nameElement.setText(siteName); if (NullChecker.isNotEmpty(expectedCount)) { QName expectedCountQname = new QName(this.dasextensionNamespace, "expectedCount"); ExtensibleElement expectedCountElement = site.addExtension(expectedCountQname); expectedCountElement.setText(expectedCount); } if (NullChecker.isNotEmpty(retrievedCount)) { QName retrievedCountQname = new QName(this.dasextensionNamespace, "retrievedCount"); ExtensibleElement retrievedCountElement = site.addExtension(retrievedCountQname); retrievedCountElement.setText(retrievedCount); } QName statusQname = new QName(this.dasextensionNamespace, "status"); ExtensibleElement statusElement = site.addExtension(statusQname); String siteStatus = new Integer(siteHttpStatus).toString(); statusElement.setText(siteStatus); if (NullChecker.isNotEmpty(siteErrorMessage)) { QName errorQname = new QName(this.dasextensionNamespace, "error"); ExtensibleElement siteErrorElement = site.addExtension(errorQname); siteErrorElement.setText(siteErrorMessage); } } } return feed; }