From 5fc1c8c4b827cd41dc8a2c11f6cacf1236d33bcd Mon Sep 17 00:00:00 2001 From: relves Date: Sat, 20 Mar 2010 15:29:25 +0000 Subject: NEW - bug 306604: [oslc] support ChangeRequest creation https://bugs.eclipse.org/bugs/show_bug.cgi?id=306604 --- .../internal/oslc/core/IOslcCoreConstants.java | 15 +++- .../oslc/core/client/AbstractOslcClient.java | 100 ++++++++++++++++++++- .../eclipse/mylyn/oslc/cm/tests/RtcOlscTest.java | 22 ++++- 3 files changed, 130 insertions(+), 7 deletions(-) diff --git a/org.eclipse.mylyn.oslc.core/src/org/eclipse/mylyn/internal/oslc/core/IOslcCoreConstants.java b/org.eclipse.mylyn.oslc.core/src/org/eclipse/mylyn/internal/oslc/core/IOslcCoreConstants.java index 275d17bea..9bbb77146 100644 --- a/org.eclipse.mylyn.oslc.core/src/org/eclipse/mylyn/internal/oslc/core/IOslcCoreConstants.java +++ b/org.eclipse.mylyn.oslc.core/src/org/eclipse/mylyn/internal/oslc/core/IOslcCoreConstants.java @@ -22,17 +22,26 @@ public interface IOslcCoreConstants { //Namespaces - public static final Namespace NAMESPACE_OSLC_CM_1_0 = Namespace.getNamespace("http://open-services.net/xmlns/cm/1.0/"); //$NON-NLS-1$ + public static final Namespace NAMESPACE_OSLC_CM_1_0 = Namespace.getNamespace( + "oslc_cm", "http://open-services.net/xmlns/cm/1.0/"); //$NON-NLS-1$ //$NON-NLS-2$ - public static final Namespace NAMESPACE_OSLC_DISCOVERY_1_0 = Namespace.getNamespace("http://open-services.net/xmlns/discovery/1.0/"); //$NON-NLS-1$ + public static final Namespace NAMESPACE_OSLC_DISCOVERY_1_0 = Namespace.getNamespace( + "oslc_disc", "http://open-services.net/xmlns/discovery/1.0/"); //$NON-NLS-1$ //$NON-NLS-2$ - public static final Namespace NAMESPACE_DC = Namespace.getNamespace("http://purl.org/dc/terms/"); //$NON-NLS-1$ + public static final Namespace NAMESPACE_DC = Namespace.getNamespace("dc", "http://purl.org/dc/terms/"); //$NON-NLS-1$ public static final Namespace NAMESPACE_RDF = Namespace.getNamespace( "rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); //$NON-NLS-1$ //$NON-NLS-2$ public static final Namespace NAMESPACE_ATOM = Namespace.getNamespace("atom", "http://www.w3.org/2005/Atom"); //$NON-NLS-1$ //$NON-NLS-2$ + public static final Namespace NAMESPACE_RTC_CM_1_0 = Namespace.getNamespace( + "rtc_cm", "http://jazz.net/xmlns/prod/jazz/rtc/cm/1.0/"); //$NON-NLS-1$ //$NON-NLS-2$ + + // Content types + + public static final String CONTENT_TYPE_CHANGE_REQUEST = "application/x-oslc-cm-change-request+xml"; //$NON-NLS-1$ + // XML element ids public static final String ELEMENT_SERVICE_PROVIDER_CATALOG = "ServiceProviderCatalog"; //$NON-NLS-1$ diff --git a/org.eclipse.mylyn.oslc.core/src/org/eclipse/mylyn/internal/oslc/core/client/AbstractOslcClient.java b/org.eclipse.mylyn.oslc.core/src/org/eclipse/mylyn/internal/oslc/core/client/AbstractOslcClient.java index 4ffe32139..2c1081cf9 100644 --- a/org.eclipse.mylyn.oslc.core/src/org/eclipse/mylyn/internal/oslc/core/client/AbstractOslcClient.java +++ b/org.eclipse.mylyn.oslc.core/src/org/eclipse/mylyn/internal/oslc/core/client/AbstractOslcClient.java @@ -13,6 +13,7 @@ package org.eclipse.mylyn.internal.oslc.core.client; import java.io.IOException; import java.io.InputStream; +import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; @@ -20,6 +21,7 @@ import java.util.List; import java.util.Set; import org.apache.commons.httpclient.Credentials; +import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HostConfiguration; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpMethodBase; @@ -29,10 +31,12 @@ import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.PutMethod; +import org.apache.commons.httpclient.methods.StringRequestEntity; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.SubProgressMonitor; import org.eclipse.mylyn.commons.net.AbstractWebLocation; import org.eclipse.mylyn.commons.net.AuthenticationCredentials; import org.eclipse.mylyn.commons.net.AuthenticationType; @@ -48,6 +52,7 @@ import org.eclipse.mylyn.internal.oslc.core.OslcServiceProviderCatalog; import org.eclipse.mylyn.internal.oslc.core.ServiceHome; import org.eclipse.mylyn.internal.oslc.core.cm.AbstractChangeRequest; import org.eclipse.mylyn.tasks.core.RepositoryResponse; +import org.eclipse.mylyn.tasks.core.RepositoryResponse.ResponseKind; import org.eclipse.mylyn.tasks.core.data.TaskAttribute; import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper; import org.eclipse.mylyn.tasks.core.data.TaskData; @@ -57,6 +62,8 @@ import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.filter.ElementFilter; import org.jdom.input.SAXBuilder; +import org.jdom.output.Format; +import org.jdom.output.XMLOutputter; /** * Base class from which to implement an OSLC client @@ -65,6 +72,8 @@ import org.jdom.input.SAXBuilder; */ public abstract class AbstractOslcClient { + private static final String UTF_8 = "UTF-8"; //$NON-NLS-1$ + protected final AbstractWebLocation location; protected final HttpClient httpClient; @@ -293,8 +302,9 @@ public abstract class AbstractOslcClient { .getText(); String url = element.getChild(IOslcCoreConstants.ELEMENT_URL, IOslcCoreConstants.NAMESPACE_OSLC_CM_1_0) .getText(); - if (element.getAttribute(IOslcCoreConstants.ATTRIBUTE_DEFAULT) != null - && element.getAttribute(IOslcCoreConstants.ATTRIBUTE_DEFAULT).getValue().equals("true")) { //$NON-NLS-1$ + Attribute attrDefault = element.getAttribute(IOslcCoreConstants.ATTRIBUTE_DEFAULT, + IOslcCoreConstants.NAMESPACE_OSLC_CM_1_0); + if (attrDefault != null && attrDefault.getValue().equals("true")) { //$NON-NLS-1$ isDefault = true; } OslcServiceFactory factory = new OslcServiceFactory(title, url); @@ -359,6 +369,15 @@ public abstract class AbstractOslcClient { } + protected OslcServiceDescriptor getConfiguration(IProgressMonitor monitor) throws CoreException { + monitor = Policy.monitorFor(monitor); + if (configuration.getFactories().isEmpty()) { + updateRepositoryConfiguration(new SubProgressMonitor(monitor, 1)); + } + + return configuration; + } + public Collection performQuery(String queryUrl, IProgressMonitor monitor) throws CoreException { @@ -424,6 +443,81 @@ public abstract class AbstractOslcClient { public abstract RepositoryResponse putTaskData(TaskData taskData, Set oldValues, IProgressMonitor monitor) throws CoreException; + public RepositoryResponse postTaskData(TaskData taskData, IProgressMonitor monitor) throws CoreException { + + Element root = new Element(IOslcCoreConstants.ELEMENT_CHANGEREQUEST, IOslcCoreConstants.NAMESPACE_OSLC_CM_1_0); + final Document doc = new Document(root); + + TaskAttribute attribute = taskData.getRoot().getMappedAttribute(IOslcCoreConstants.ELEMENT_TITLE); + if (attribute != null) { + // TODO: Store namespace on attribute + Element e = new Element(attribute.getId(), IOslcCoreConstants.NAMESPACE_DC); + e.setText(attribute.getValue()); + root.addContent(e); + } + + attribute = taskData.getRoot().getMappedAttribute(IOslcCoreConstants.ELEMENT_DESCRIPTION); + if (attribute != null) { + Element e = new Element(attribute.getId(), IOslcCoreConstants.NAMESPACE_DC); + e.setText(attribute.getValue()); + root.addContent(e); + } + + attribute = taskData.getRoot().getMappedAttribute(IOslcCoreConstants.ELEMENT_TYPE); + if (attribute != null) { + Element e = new Element(attribute.getId(), IOslcCoreConstants.NAMESPACE_DC); + e.setText(attribute.getValue()); + root.addContent(e); + } + + attribute = taskData.getRoot().getMappedAttribute(IOslcCoreConstants.ELEMENT_SUBJECT); + if (attribute != null) { + Element e = new Element(attribute.getId(), IOslcCoreConstants.NAMESPACE_DC); + e.setText(attribute.getValue()); + root.addContent(e); + } + + // For RTC Tests + attribute = taskData.getRoot().getMappedAttribute("filedAgainst"); //$NON-NLS-1$ + if (attribute != null) { + Element e = new Element(attribute.getId(), IOslcCoreConstants.NAMESPACE_RTC_CM_1_0); + e.setText(attribute.getValue()); + root.addContent(e); + } + + PostMethod method = createPostMethod(getConfiguration(monitor).getDefaultFactory().getUrl()); + method.setRequestHeader("Accept", "application/x-oslc-cm-change-request+xml"); //$NON-NLS-1$ //$NON-NLS-2$ + method.setRequestHeader("Content-Type", "application/x-oslc-cm-change-request+xml"); //$NON-NLS-1$ //$NON-NLS-2$ + + XMLOutputter out = new XMLOutputter(Format.getCompactFormat()); + try { + method.setRequestEntity(new StringRequestEntity(out.outputString(doc), + IOslcCoreConstants.CONTENT_TYPE_CHANGE_REQUEST, UTF_8)); + } catch (UnsupportedEncodingException e1) { + throw new CoreException(new Status(IStatus.ERROR, IOslcCoreConstants.ID_PLUGIN, + "Error creating new change request: " + e1.getMessage(), e1)); //$NON-NLS-1$ + } + + RequestHandler handler = new RequestHandler("Creating") { //$NON-NLS-1$ + + @Override + public RepositoryResponse run(HttpMethodBase method, IProgressMonitor monitor) throws CoreException { + Header header = method.getResponseHeader("Location"); //$NON-NLS-1$ + + if (header != null && header.getValue() != null) { + + String location = header.getValue(); + // TODO: delegate extraction of 'task id' to protected method and add to repository response + return new RepositoryResponse(ResponseKind.TASK_CREATED, location); + } + return null; + } + }; + + return executeMethod(method, handler, monitor); + + } + protected GetMethod createGetMethod(String requestPath) { GetMethod method = new GetMethod(getRequestPath(requestPath)); method.setFollowRedirects(true); @@ -444,7 +538,7 @@ public abstract class AbstractOslcClient { // method.setRequestEntity(entity); // } else { // StatusHandler.log(new Status(IStatus.WARNING, IOslcCoreConstants.ID_PLUGIN, -// "Request body or entity missing upon post.")); //$NON-NLS-1$ +// "Request body or entity missing upon post.")); // } return method; } diff --git a/org.eclipse.mylyn.oslc.tests/src/org/eclipse/mylyn/oslc/cm/tests/RtcOlscTest.java b/org.eclipse.mylyn.oslc.tests/src/org/eclipse/mylyn/oslc/cm/tests/RtcOlscTest.java index cf3568b2a..1cb8f0c65 100644 --- a/org.eclipse.mylyn.oslc.tests/src/org/eclipse/mylyn/oslc/cm/tests/RtcOlscTest.java +++ b/org.eclipse.mylyn.oslc.tests/src/org/eclipse/mylyn/oslc/cm/tests/RtcOlscTest.java @@ -50,7 +50,9 @@ public class RtcOlscTest extends TestCase { private AbstractOslcClient client; - private static final String BASE_URL = "https://172.16.166.130:9443/jazz/oslc/workitems/catalog"; + private static final String BASE_URL = "https://192.168.0.3:9443/jazz/oslc/workitems/catalog"; + +// private static final String BASE_URL = "https://192.168.0.3:9443/jazz/oslc/contexts/_9Dyg4DLzEd-G-8cuiS4gvg/workitems/services.xml"; @Override protected void setUp() throws Exception { @@ -89,6 +91,24 @@ public class RtcOlscTest extends TestCase { }; } + // Test Passing when resource urls below match up with your particular RTC instance + // and when BASE_URL set to service url. + /*public void testChangeRequestCreation() throws Exception { + TaskData data = new TaskData(new TaskAttributeMapper(repository), "oslc", BASE_URL, ""); + data.getRoot().createAttribute(IOslcCoreConstants.ELEMENT_TITLE).setValue("New Title"); + data.getRoot().createAttribute(IOslcCoreConstants.ELEMENT_TYPE).setValue( + "https://192.168.0.3:9443/jazz/oslc/types/_9Dyg4DLzEd-G-8cuiS4gvg/defect"); + data.getRoot().createAttribute(IOslcCoreConstants.ELEMENT_DESCRIPTION).setValue("New Description"); + data.getRoot().createAttribute(IOslcCoreConstants.ELEMENT_SUBJECT).setValue("New Subject"); + data.getRoot() + .createAttribute("filedAgainst") + .setValue( + "https://192.168.0.3:9443/jazz/resource/itemOid/com.ibm.team.workitem.Category/_9cYnETLzEd-G-8cuiS4gvg"); + + RepositoryResponse response = client.postTaskData(data, null); + assertNotNull(response); + }*/ + /** * Service Discovery */ -- cgit v1.2.3