Created
April 4, 2012 19:50
-
-
Save nialdarbey/2305102 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Mule Development Kit | |
* Copyright 2010-2011 (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
/** | |
* This file was automatically generated by the Mule Development Kit | |
*/ | |
package com.mulesoft.se; | |
import javax.ws.rs.core.MediaType; | |
import org.mule.api.ConnectionException; | |
import org.mule.api.ConnectionExceptionCode; | |
import org.mule.api.annotations.Configurable; | |
import org.mule.api.annotations.Connect; | |
import org.mule.api.annotations.ConnectionIdentifier; | |
import org.mule.api.annotations.Connector; | |
import org.mule.api.annotations.Disconnect; | |
import org.mule.api.annotations.Processor; | |
import org.mule.api.annotations.ValidateConnection; | |
import org.mule.api.annotations.param.ConnectionKey; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import com.sun.jersey.api.client.Client; | |
import com.sun.jersey.api.client.WebResource; | |
/** | |
* Cloud Connector | |
* | |
* @author MuleSoft, Inc. | |
*/ | |
@Connector(name = "pivotaltracker", schemaVersion = "1.0") | |
public class PivotalTrackerConnector { | |
private Logger logger = LoggerFactory.getLogger(PivotalTrackerConnector.class); | |
/** | |
* Configurable | |
*/ | |
@Configurable | |
private String trackerToken; | |
/** | |
* Configurable | |
*/ | |
@Configurable | |
private String storiesUrl; | |
public void setTrackerToken(String trackerToken) { | |
this.trackerToken = trackerToken; | |
} | |
public void setStoriesUrl(String storiesUrl) { | |
this.storiesUrl = storiesUrl; | |
} | |
/** | |
* Connect | |
* | |
* @param username | |
* A username | |
* @param password | |
* A password | |
* @throws ConnectionException | |
*/ | |
@Connect | |
public void connect(@ConnectionKey String username, String password) throws ConnectionException { | |
/* | |
* CODE FOR ESTABLISHING A CONNECTION GOES IN HERE | |
*/ | |
if (false) { | |
throw new ConnectionException(ConnectionExceptionCode.INCORRECT_CREDENTIALS, "Wrong password!", "Sorry"); | |
} | |
} | |
/** | |
* Disconnect | |
*/ | |
@Disconnect | |
public void disconnect() { | |
/* | |
* CODE FOR CLOSING A CONNECTION GOES IN HERE | |
*/ | |
} | |
/** | |
* Are we connected | |
*/ | |
@ValidateConnection | |
public boolean isConnected() { | |
return true; | |
} | |
/** | |
* Are we connected | |
*/ | |
@ConnectionIdentifier | |
public String connectionId() { | |
return "001"; | |
} | |
/** | |
* Creates a story on Pivotal TRacker | |
* | |
* {@sample.xml ../../../doc/PivotalTracker-connector.xml.sample | |
* pivotaltracker:create-story} | |
* | |
* @param storyXml | |
* details of story | |
* @return Story xml | |
*/ | |
@Processor | |
public String createStory(String storyXml) { | |
logger.info("Creating story"); | |
Client client = Client.create(); | |
WebResource webResource = client.resource(storiesUrl); | |
storyXml = webResource.type(MediaType.APPLICATION_XML).header("X-TrackerToken", trackerToken).header("Content-type", "application/xml").post(String.class, storyXml); | |
return storyXml; | |
} | |
/** | |
* Adds a label to a Story on Pivotal Tracker | |
* | |
* {@sample.xml ../../../doc/PivotalTracker-connector.xml.sample | |
* pivotaltracker:label-story} | |
* | |
* @param labelXml | |
* details of label | |
* @param storyId | |
* id of story to update | |
* @return Story xml | |
*/ | |
@Processor | |
public String labelStory(String labelXml, String storyId) { | |
Client client = Client.create(); | |
WebResource webResource = client.resource(storiesUrl + "/" + storyId); | |
String storyXml = webResource.type(MediaType.APPLICATION_XML).header("X-TrackerToken", trackerToken).header("Content-type", "application/xml").put(String.class, labelXml); | |
return storyXml; | |
} | |
/** | |
* Finds a Story on Pivotal Tracker based on criteria in filter | |
* | |
* {@sample.xml ../../../doc/PivotalTracker-connector.xml.sample | |
* pivotaltracker:find-story-by-filter} | |
* | |
* @param filter | |
* search criteria | |
* @return Search results as stories xml | |
*/ | |
@Processor | |
public String findStoryByFilter(String filter) { | |
Client client = Client.create(); | |
WebResource webResource = client.resource(storiesUrl + "?filter=" + escape(filter)); | |
String storyXml = webResource.header("X-TrackerToken", trackerToken).get(String.class); | |
return storyXml; | |
} | |
private CharSequence escape(String string) { | |
return string.replaceAll("%", "%25").replaceAll(" ", "%20").replaceAll("\"", "%22").replaceAll("\\-", "%2D").replaceAll("\\.", "%2E").replaceAll("<", "%3C").replaceAll(">", "%3E").replaceAll("\\\\", "%5C").replaceAll("\\^", "%5E").replaceAll("_", "%5F").replaceAll("`", "%60").replaceAll("\\{", "%7B").replaceAll("\\|", "%7C").replaceAll("\\}", "%7D").replaceAll("~", "%7E") | |
.replaceAll("!", "%21").replaceAll("#", "%23").replaceAll("\\$", "%24").replaceAll("&", "%26").replaceAll("'", "%27").replaceAll("\\(", "%28").replaceAll("\\)", "%29").replaceAll("\\*", "%2A").replaceAll("\\+", "%2B").replaceAll(",", "%2C").replaceAll("/", "%2F").replaceAll(":", "%3A").replaceAll(";", "%3B").replaceAll("=", "%3D").replaceAll("\\?", "%3F").replaceAll("@", "%40").replaceAll("\\[", "%5B").replaceAll("\\]", "%5D"); | |
} | |
/** | |
* Comments a Story on Pivotal Tracker | |
* | |
* {@sample.xml ../../../doc/PivotalTracker-connector.xml.sample | |
* pivotaltracker:comment-story} | |
* | |
* @param storyId | |
* id of story to update | |
* @param commentsXml | |
* details of comment | |
* @return Story xml | |
*/ | |
@Processor | |
public String commentStory(String commentsXml, String storyId) { | |
Client client = Client.create(); | |
WebResource webResource = client.resource(storiesUrl + "/" + storyId + "/notes"); | |
String storyXml = webResource.type(MediaType.APPLICATION_XML).header("X-TrackerToken", trackerToken).header("Content-type", "application/xml").post(String.class, commentsXml); | |
return storyXml; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment