summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamuel Padgett2011-11-11 15:34:17 (EST)
committer Michael Fiedler2012-01-10 15:22:12 (EST)
commit08db5326ace380900d0f15b0c89f0209eed0cc6f (patch)
tree62f55916bf77f3da0a0935b17912c1b442e6136e
parent594d0a46d82b64e4565eb8dc4f9ae5696ed3f895 (diff)
downloadorg.eclipse.lyo.server-08db5326ace380900d0f15b0c89f0209eed0cc6f.zip
org.eclipse.lyo.server-08db5326ace380900d0f15b0c89f0209eed0cc6f.tar.gz
org.eclipse.lyo.server-08db5326ace380900d0f15b0c89f0209eed0cc6f.tar.bz2
Bug 362050 - [Bugzilla] Add support for Basic auth
-rw-r--r--org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/BugzillaInitializer.java56
-rw-r--r--org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ChangeRequestCollectionService.java24
-rw-r--r--org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ChangeRequestCreatorService.java9
-rw-r--r--org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ChangeRequestSelectorService.java6
-rw-r--r--org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ChangeRequestService.java13
-rw-r--r--org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/CreationShapeService.java7
-rw-r--r--org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/Credentials.java42
-rw-r--r--org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/QueryShapeService.java7
-rw-r--r--org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ServiceProviderCatalogService.java4
-rw-r--r--org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ServiceProviderService.java7
-rw-r--r--org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/exception/RestException.java47
-rw-r--r--org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/exception/UnauthroziedException.java34
-rw-r--r--org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/resources/Error.java68
-rw-r--r--org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/utils/HttpUtils.java101
-rw-r--r--org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/utils/RdfUtils.java20
-rw-r--r--org.eclipse.lyo.samples.bugzilla/src/test/java/org/eclipse/lyo/samples/bugzilla/test/TestConnection.java35
16 files changed, 421 insertions, 59 deletions
diff --git a/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/BugzillaInitializer.java b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/BugzillaInitializer.java
index 9af7cc0..2a958ad 100644
--- a/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/BugzillaInitializer.java
+++ b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/BugzillaInitializer.java
@@ -27,30 +27,27 @@ import jbugz.exceptions.BugzillaException;
import jbugz.exceptions.ConnectionException;
import jbugz.rpc.LogIn;
+import org.eclipse.lyo.samples.bugzilla.exception.UnauthroziedException;
+import org.eclipse.lyo.samples.bugzilla.utils.HttpUtils;
+
public class BugzillaInitializer {
private static final String CONNECTOR_SESSION_ATTRIBUTE = "org.eclipse.lyo.samples.bugzilla.BugzillaConnector";
private static String baseUri = null;
private static String bugzillaUri = null;
- private static String username = null;
- private static String password = null;
private static boolean provideHtml = true;
-
+
static {
Properties props = new Properties();
try {
props.load(BugzillaInitializer.class.getResourceAsStream("/bugz.properties"));
baseUri = props.getProperty("adapter_uri");
bugzillaUri = props.getProperty("bugzilla_uri");
- username = props.getProperty("username");
- password = props.getProperty("password");
if (props.getProperty("provideHtml") != null) {
provideHtml = Boolean.parseBoolean(props.getProperty("provideHtml"));
}
System.out.println("adapter_uri: " + baseUri);
System.out.println("bugzilla_uri: " + bugzillaUri);
- System.out.println("username: " + username);
- System.out.println("password: " + password);
System.out.println("provideHtml: " + provideHtml);
} catch (IOException e) {
@@ -58,29 +55,38 @@ public class BugzillaInitializer {
}
}
- public static BugzillaConnector getBugzillaConnector() throws ConnectionException, BugzillaException {
- BugzillaConnector bc = new BugzillaConnector();
- bc.connectTo(bugzillaUri + "/xmlrpc.cgi");
- LogIn login = new LogIn(username, password);
- bc.executeMethod(login);
- return bc;
+ public static BugzillaConnector getBugzillaConnector(Credentials credentials)
+ throws ConnectionException, UnauthroziedException {
+ BugzillaConnector bc = new BugzillaConnector();
+ bc.connectTo(bugzillaUri + "/xmlrpc.cgi");
+ LogIn login = new LogIn(credentials.getUsername(), credentials.getPassword());
+ try {
+ bc.executeMethod(login);
+ } catch (BugzillaException e) {
+ throw new UnauthroziedException(e.getCause().getMessage());
+ }
+ return bc;
}
public static BugzillaConnector getBugzillaConnector(
HttpServletRequest request) throws ConnectionException,
- BugzillaException {
+ UnauthroziedException {
HttpSession session = request.getSession();
BugzillaConnector connector = (BugzillaConnector) session
.getAttribute(CONNECTOR_SESSION_ATTRIBUTE);
if (connector == null) {
- connector = getBugzillaConnector();
+ Credentials credentials = HttpUtils.getCredentials(request);
+ if (credentials == null) {
+ throw new UnauthroziedException();
+ }
+ connector = getBugzillaConnector(credentials);
session.setAttribute(CONNECTOR_SESSION_ATTRIBUTE, connector);
}
return connector;
}
-
- public static String getBaseUri() {
+
+ public static String getBaseUri() {
return baseUri;
}
@@ -96,22 +102,6 @@ public class BugzillaInitializer {
BugzillaInitializer.bugzillaUri = bugzillaUri;
}
- public static String getUsername() {
- return username;
- }
-
- public static void setUsername(String username) {
- BugzillaInitializer.username = username;
- }
-
- public static String getPassword() {
- return password;
- }
-
- public static void setPassword(String password) {
- BugzillaInitializer.password = password;
- }
-
public static boolean isProvideHtml() {
return provideHtml;
}
diff --git a/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ChangeRequestCollectionService.java b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ChangeRequestCollectionService.java
index 8a9b141..299ddbf 100644
--- a/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ChangeRequestCollectionService.java
+++ b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ChangeRequestCollectionService.java
@@ -34,6 +34,7 @@ import jbugz.exceptions.ConnectionException;
import jbugz.exceptions.InvalidDescriptionException;
import jbugz.rpc.ReportBug;
+import org.eclipse.lyo.samples.bugzilla.exception.UnauthroziedException;
import org.eclipse.lyo.samples.bugzilla.jbugzx.base.Product;
import org.eclipse.lyo.samples.bugzilla.jbugzx.rpc.ExtendedBugSearch;
import org.eclipse.lyo.samples.bugzilla.jbugzx.rpc.GetProducts;
@@ -42,6 +43,7 @@ import org.eclipse.lyo.samples.bugzilla.resources.Person;
import org.eclipse.lyo.samples.bugzilla.resources.QueryResponse;
import org.eclipse.lyo.samples.bugzilla.resources.ResponseInfo;
import org.eclipse.lyo.samples.bugzilla.utils.AcceptType;
+import org.eclipse.lyo.samples.bugzilla.utils.HttpUtils;
import org.eclipse.lyo.samples.bugzilla.utils.RdfUtils;
import thewebsemantic.Bean2RDF;
@@ -56,7 +58,7 @@ import com.hp.hpl.jena.rdf.model.ModelFactory;
*/
public class ChangeRequestCollectionService extends HttpServlet {
private static final long serialVersionUID = -5280734755943517104L;
-
+
public ChangeRequestCollectionService() {}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
@@ -83,15 +85,20 @@ public class ChangeRequestCollectionService extends HttpServlet {
createChangeRequests(request, response, changeRequests);
response.setHeader("OSLC-Core-Version", "2.0");
response.setStatus(HttpServletResponse.SC_CREATED);
+ } catch (UnauthroziedException e) {
+ HttpUtils.sendUnauthorizedResponse(response, e);
} catch (Exception e) {
throw new ServletException(e);
}
}
- private void createChangeRequests(HttpServletRequest request, HttpServletResponse response,
+ private void createChangeRequests(HttpServletRequest request,
+ HttpServletResponse response,
Collection<BugzillaChangeRequest> changeRequests)
- throws ConnectionException, BugzillaException, InvalidDescriptionException {
- BugzillaConnector bc = BugzillaInitializer.getBugzillaConnector(request);
+ throws ConnectionException, BugzillaException,
+ InvalidDescriptionException, UnauthroziedException {
+ BugzillaConnector bc = BugzillaInitializer
+ .getBugzillaConnector(request);
for (BugzillaChangeRequest cr : changeRequests) {
ReportBug reportBug = new ReportBug(cr.toBug());
@@ -139,6 +146,9 @@ public class ChangeRequestCollectionService extends HttpServlet {
List<Product> products = getProducts.getProducts();
product = products.get(0);
+ } catch (UnauthroziedException e) {
+ HttpUtils.sendUnauthorizedResponse(response, e);
+ return;
} catch (Exception e) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
@@ -196,9 +206,7 @@ public class ChangeRequestCollectionService extends HttpServlet {
QueryResponse queryResult = new QueryResponse();
// This must match the query capability base.
- queryResult.setUri(new URI(request.getRequestURL()
- .append("?productId=").append(product.getId())
- .toString()));
+ queryResult.setUri(new URI(URLStrategy.getChangeRequestCollectionURL(product.getId())));
for (Bug bug : results) {
BugzillaChangeRequest changeRequest = BugzillaChangeRequest.fromBug(bug);
queryResult.getMembers().add(changeRequest);
@@ -213,6 +221,8 @@ public class ChangeRequestCollectionService extends HttpServlet {
} else {
response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
}
+ } catch (UnauthroziedException e) {
+ HttpUtils.sendUnauthorizedResponse(response, e);
} catch (Throwable e) {
throw new ServletException(e);
}
diff --git a/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ChangeRequestCreatorService.java b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ChangeRequestCreatorService.java
index 35e6b06..e123721 100644
--- a/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ChangeRequestCreatorService.java
+++ b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ChangeRequestCreatorService.java
@@ -33,9 +33,11 @@ import jbugz.base.Bug;
import jbugz.base.BugzillaConnector;
import jbugz.rpc.ReportBug;
+import org.eclipse.lyo.samples.bugzilla.exception.UnauthroziedException;
import org.eclipse.lyo.samples.bugzilla.jbugzx.base.Product;
import org.eclipse.lyo.samples.bugzilla.jbugzx.rpc.GetLegalValues;
import org.eclipse.lyo.samples.bugzilla.jbugzx.rpc.GetProducts;
+import org.eclipse.lyo.samples.bugzilla.utils.HttpUtils;
import org.eclipse.lyo.samples.bugzilla.utils.StringUtils;
@@ -63,6 +65,9 @@ public class ChangeRequestCreatorService extends HttpServlet {
product = products.get(0);
request.setAttribute("product", product);
+ } catch (UnauthroziedException e) {
+ HttpUtils.sendUnauthorizedResponse(response, e);
+ return;
} catch (Exception e) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
@@ -97,6 +102,8 @@ public class ChangeRequestCreatorService extends HttpServlet {
RequestDispatcher rd = request.getRequestDispatcher("/cm/changerequest_creator.jsp");
rd.forward(request, response);
+ } catch (UnauthroziedException e) {
+ HttpUtils.sendUnauthorizedResponse(response, e);
} catch (Exception e) {
throw new ServletException(e);
}
@@ -157,6 +164,8 @@ public class ChangeRequestCreatorService extends HttpServlet {
"\"resource\" : \"" + URLStrategy.getChangeRequestURL(reportBug.getID()) + "\"}");
out.close();
+ } catch (UnauthroziedException e) {
+ HttpUtils.sendUnauthorizedResponse(response, e);
} catch (Exception e) {
throw new ServletException(e);
}
diff --git a/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ChangeRequestSelectorService.java b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ChangeRequestSelectorService.java
index 6d53788..78a7a26 100644
--- a/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ChangeRequestSelectorService.java
+++ b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ChangeRequestSelectorService.java
@@ -29,7 +29,9 @@ import jbugz.base.Bug;
import jbugz.base.BugzillaConnector;
import jbugz.rpc.BugSearch;
+import org.eclipse.lyo.samples.bugzilla.exception.UnauthroziedException;
import org.eclipse.lyo.samples.bugzilla.jbugzx.rpc.ExtendedBugSearch;
+import org.eclipse.lyo.samples.bugzilla.utils.HttpUtils;
/**
@@ -72,7 +74,7 @@ public class ChangeRequestSelectorService extends HttpServlet {
private void sendFilteredBugsReponse(
int productId, String terms, HttpServletRequest request, HttpServletResponse response)
- throws ServletException {
+ throws ServletException, IOException {
try {
BugzillaConnector bc = BugzillaInitializer.getBugzillaConnector(request);
ExtendedBugSearch bugSearch = new ExtendedBugSearch(BugSearch.SearchLimiter.SUMMARY, terms);
@@ -83,6 +85,8 @@ public class ChangeRequestSelectorService extends HttpServlet {
RequestDispatcher rd = request.getRequestDispatcher("/cm/changerequest_filtered_json.jsp");
rd.forward(request, response);
+ } catch (UnauthroziedException e) {
+ HttpUtils.sendUnauthorizedResponse(response, e);
} catch (Exception e) {
throw new ServletException(e);
}
diff --git a/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ChangeRequestService.java b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ChangeRequestService.java
index 4a6e759..7d3c224 100644
--- a/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ChangeRequestService.java
+++ b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ChangeRequestService.java
@@ -36,10 +36,12 @@ import jbugz.exceptions.ConnectionException;
import jbugz.exceptions.InvalidDescriptionException;
import jbugz.rpc.CommentBug;
+import org.eclipse.lyo.samples.bugzilla.exception.UnauthroziedException;
import org.eclipse.lyo.samples.bugzilla.jbugzx.rpc.ExtendedGetBug;
import org.eclipse.lyo.samples.bugzilla.resources.BugzillaChangeRequest;
import org.eclipse.lyo.samples.bugzilla.resources.Person;
import org.eclipse.lyo.samples.bugzilla.utils.AcceptType;
+import org.eclipse.lyo.samples.bugzilla.utils.HttpUtils;
import org.eclipse.lyo.samples.bugzilla.utils.RdfUtils;
import thewebsemantic.RDF2Bean;
@@ -91,6 +93,9 @@ public class ChangeRequestService extends HttpServlet {
e.printStackTrace();
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
+ } catch (UnauthroziedException e) {
+ HttpUtils.sendUnauthorizedResponse(response, e);
+ return;
}
try {
@@ -114,13 +119,15 @@ public class ChangeRequestService extends HttpServlet {
|| AcceptType.willAccept("application/xml", request)) {
response.setHeader("OSLC-Core-Version", "2.0");
response.setHeader("Content-Type", "application/rdf+xml");
- RdfUtils.sendRdfResponse(response, bug, RdfUtils.JENA_LANG_ABBREVIATED_RDF_XML);
+ BugzillaChangeRequest changeRequest = BugzillaChangeRequest.fromBug(bug);
+ RdfUtils.sendRdfResponse(response, changeRequest, RdfUtils.JENA_LANG_ABBREVIATED_RDF_XML);
return;
} else if (AcceptType.willAccept("text/turtle", request)) {
response.setHeader("OSLC-Core-Version", "2.0");
response.setHeader("Content-Type", "text/turtle");
- RdfUtils.sendRdfResponse(response, bug, RdfUtils.JENA_LANG_TURTLE);
+ BugzillaChangeRequest changeRequest = BugzillaChangeRequest.fromBug(bug);
+ RdfUtils.sendRdfResponse(response, changeRequest, RdfUtils.JENA_LANG_TURTLE);
return;
} else {
@@ -169,7 +176,7 @@ public class ChangeRequestService extends HttpServlet {
private void updateBug(HttpServletRequest request, BugzillaChangeRequest cr)
throws ConnectionException, BugzillaException,
- InvalidDescriptionException {
+ InvalidDescriptionException, UnauthroziedException {
BugzillaConnector bc = BugzillaInitializer.getBugzillaConnector(request);
// No built in field to hold external links. Just add the new link as a comment for now.
String comment = getLinksComment(cr);
diff --git a/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/CreationShapeService.java b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/CreationShapeService.java
index f76d9ee..06203f0 100644
--- a/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/CreationShapeService.java
+++ b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/CreationShapeService.java
@@ -28,11 +28,13 @@ import javax.servlet.http.HttpServletResponse;
import jbugz.base.BugzillaConnector;
+import org.eclipse.lyo.samples.bugzilla.exception.UnauthroziedException;
import org.eclipse.lyo.samples.bugzilla.jbugzx.base.Product;
import org.eclipse.lyo.samples.bugzilla.jbugzx.rpc.GetAccessibleProducts;
import org.eclipse.lyo.samples.bugzilla.jbugzx.rpc.GetLegalValues;
import org.eclipse.lyo.samples.bugzilla.jbugzx.rpc.GetProducts;
import org.eclipse.lyo.samples.bugzilla.utils.AcceptType;
+import org.eclipse.lyo.samples.bugzilla.utils.HttpUtils;
/**
@@ -58,6 +60,9 @@ public class CreationShapeService extends HttpServlet {
List<Product> products = getProducts.getProducts();
request.setAttribute("product", products.get(0));
+ } catch (UnauthroziedException e) {
+ HttpUtils.sendUnauthorizedResponse(response, e);
+ return;
} catch (Exception e) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
@@ -111,6 +116,8 @@ public class CreationShapeService extends HttpServlet {
rd.forward(request, response);
response.flushBuffer();
+ } catch (UnauthroziedException e) {
+ HttpUtils.sendUnauthorizedResponse(response, e);
} catch (Throwable e) {
throw new ServletException(e);
}
diff --git a/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/Credentials.java b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/Credentials.java
new file mode 100644
index 0000000..39a561b
--- /dev/null
+++ b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/Credentials.java
@@ -0,0 +1,42 @@
+/*******************************************************************************
+ * Copyright (c) 2011 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.lyo.samples.bugzilla;
+
+/**
+ * Encapsulates a Bugzilla username and password.
+ *
+ * @author Samuel Padgett <spadgett@us.ibm.com>
+ */
+public class Credentials {
+ private String username;
+ private String password;
+
+ public String getUsername() {
+ return username;
+ }
+
+ public void setUsername(String username) {
+ this.username = username;
+ }
+
+ public String getPassword() {
+ return password;
+ }
+
+ public void setPassword(String password) {
+ this.password = password;
+ }
+} \ No newline at end of file
diff --git a/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/QueryShapeService.java b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/QueryShapeService.java
index 55da6bc..741ed33 100644
--- a/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/QueryShapeService.java
+++ b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/QueryShapeService.java
@@ -28,11 +28,13 @@ import javax.servlet.http.HttpServletResponse;
import jbugz.base.BugzillaConnector;
+import org.eclipse.lyo.samples.bugzilla.exception.UnauthroziedException;
import org.eclipse.lyo.samples.bugzilla.jbugzx.base.Product;
import org.eclipse.lyo.samples.bugzilla.jbugzx.rpc.GetAccessibleProducts;
import org.eclipse.lyo.samples.bugzilla.jbugzx.rpc.GetLegalValues;
import org.eclipse.lyo.samples.bugzilla.jbugzx.rpc.GetProducts;
import org.eclipse.lyo.samples.bugzilla.utils.AcceptType;
+import org.eclipse.lyo.samples.bugzilla.utils.HttpUtils;
/**
@@ -60,6 +62,9 @@ public class QueryShapeService extends HttpServlet {
List<Product> products = getProducts.getProducts();
request.setAttribute("product", products.get(0));
+ } catch (UnauthroziedException e) {
+ HttpUtils.sendUnauthorizedResponse(response, e);
+ return;
} catch (Exception e) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
@@ -116,6 +121,8 @@ public class QueryShapeService extends HttpServlet {
rd.forward(request, response);
response.flushBuffer();
+ } catch (UnauthroziedException e) {
+ HttpUtils.sendUnauthorizedResponse(response, e);
} catch (Throwable e) {
throw new ServletException(e);
}
diff --git a/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ServiceProviderCatalogService.java b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ServiceProviderCatalogService.java
index c535439..cd44713 100644
--- a/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ServiceProviderCatalogService.java
+++ b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ServiceProviderCatalogService.java
@@ -27,10 +27,12 @@ import javax.servlet.http.HttpServletResponse;
import jbugz.base.BugzillaConnector;
+import org.eclipse.lyo.samples.bugzilla.exception.UnauthroziedException;
import org.eclipse.lyo.samples.bugzilla.jbugzx.base.Product;
import org.eclipse.lyo.samples.bugzilla.jbugzx.rpc.GetAccessibleProducts;
import org.eclipse.lyo.samples.bugzilla.jbugzx.rpc.GetProducts;
import org.eclipse.lyo.samples.bugzilla.utils.AcceptType;
+import org.eclipse.lyo.samples.bugzilla.utils.HttpUtils;
/**
* OSLC CM Change Request Service
@@ -78,6 +80,8 @@ public class ServiceProviderCatalogService extends HttpServlet {
rd.forward(request, response);
response.flushBuffer();
+ } catch (UnauthroziedException e) {
+ HttpUtils.sendUnauthorizedResponse(response, e);
} catch (Throwable e) {
throw new ServletException(e);
}
diff --git a/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ServiceProviderService.java b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ServiceProviderService.java
index 1e57338..a5247cd 100644
--- a/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ServiceProviderService.java
+++ b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/ServiceProviderService.java
@@ -26,10 +26,12 @@ import javax.servlet.http.HttpServletResponse;
import jbugz.base.BugzillaConnector;
+import org.eclipse.lyo.samples.bugzilla.exception.UnauthroziedException;
import org.eclipse.lyo.samples.bugzilla.jbugzx.base.Product;
import org.eclipse.lyo.samples.bugzilla.jbugzx.rpc.GetAccessibleProducts;
import org.eclipse.lyo.samples.bugzilla.jbugzx.rpc.GetProducts;
import org.eclipse.lyo.samples.bugzilla.utils.AcceptType;
+import org.eclipse.lyo.samples.bugzilla.utils.HttpUtils;
import org.eclipse.lyo.samples.bugzilla.utils.StringUtils;
@@ -57,6 +59,9 @@ public class ServiceProviderService extends HttpServlet {
List<Product> products = getProducts.getProducts();
request.setAttribute("product", products.get(0));
+ } catch (UnauthroziedException e) {
+ HttpUtils.sendUnauthorizedResponse(response, e);
+ return;
} catch (Exception e) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
@@ -94,6 +99,8 @@ public class ServiceProviderService extends HttpServlet {
rd.forward(request, response);
response.flushBuffer();
+ } catch (UnauthroziedException e) {
+ HttpUtils.sendUnauthorizedResponse(response, e);
} catch (Throwable e) {
throw new ServletException(e);
}
diff --git a/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/exception/RestException.java b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/exception/RestException.java
new file mode 100644
index 0000000..0e6f43d
--- /dev/null
+++ b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/exception/RestException.java
@@ -0,0 +1,47 @@
+/*******************************************************************************
+ * Copyright (c) 2011 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.lyo.samples.bugzilla.exception;
+
+/**
+ * Holds a status code and error message for an error response.
+ *
+ * @author Samuel Padgett <spadgett@us.ibm.com>
+ */
+public class RestException extends Exception {
+ private int statusCode;
+ private String message;
+
+ public RestException(int statusCode, String message) {
+ this.statusCode = statusCode;
+ this.message = message;
+ }
+
+ public int getStatusCode() {
+ return statusCode;
+ }
+
+ public void setStatusCode(int statusCode) {
+ this.statusCode = statusCode;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+
+ public void setMessage(String message) {
+ this.message = message;
+ }
+}
diff --git a/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/exception/UnauthroziedException.java b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/exception/UnauthroziedException.java
new file mode 100644
index 0000000..178f625
--- /dev/null
+++ b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/exception/UnauthroziedException.java
@@ -0,0 +1,34 @@
+/*******************************************************************************
+ * Copyright (c) 2011 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.lyo.samples.bugzilla.exception;
+
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * Corresponds to an HTTP 401 response.
+ *
+ * @author Samuel Padgett <spadgett@us.ibm.com>
+ */
+public class UnauthroziedException extends RestException {
+ public UnauthroziedException() {
+ super(HttpServletResponse.SC_UNAUTHORIZED,
+ "You must authenticate with Bugzilla for this request.");
+ }
+
+ public UnauthroziedException(String message) {
+ super(HttpServletResponse.SC_UNAUTHORIZED, message);
+ }
+}
diff --git a/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/resources/Error.java b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/resources/Error.java
new file mode 100644
index 0000000..4c5c6db
--- /dev/null
+++ b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/resources/Error.java
@@ -0,0 +1,68 @@
+/*******************************************************************************
+ * Copyright (c) 2011 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.lyo.samples.bugzilla.resources;
+
+import org.eclipse.lyo.samples.bugzilla.exception.RestException;
+
+import thewebsemantic.Namespace;
+import thewebsemantic.RdfProperty;
+import thewebsemantic.RdfType;
+
+/**
+ * Encapsulates an OSLC-CM 2.0 error response.
+ *
+ * @author Samuel Padgett <spadgett@us.ibm.com>
+ * @see <a href="http://open-services.net/bin/view/Main/OslcCoreSpecification?sortcol=table;up=#Error_Responses">OSLC 2.0 Error Responses</a>
+ */
+@Namespace("http://open-services.net/ns/core#")
+@RdfType("Error")
+public class Error {
+ @RdfProperty("http://open-services.net/ns/core#statusCode")
+ private int statusCode;
+ @RdfProperty("http://open-services.net/ns/core#message")
+ private String message;
+
+ public Error() {}
+
+ public static Error fromRestException(RestException e) {
+ Error error = new Error();
+ error.setMessage(e.getMessage());
+ error.setStatusCode(e.getStatusCode());
+
+ return error;
+ }
+
+ public Error(int statusCode, String message) {
+ this.statusCode = statusCode;
+ this.message = message;
+ }
+
+ public int getStatusCode() {
+ return statusCode;
+ }
+
+ public void setStatusCode(int statusCode) {
+ this.statusCode = statusCode;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+
+ public void setMessage(String message) {
+ this.message = message;
+ }
+} \ No newline at end of file
diff --git a/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/utils/HttpUtils.java b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/utils/HttpUtils.java
new file mode 100644
index 0000000..7c5d8d1
--- /dev/null
+++ b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/utils/HttpUtils.java
@@ -0,0 +1,101 @@
+/*******************************************************************************
+ * Copyright (c) 2011 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.lyo.samples.bugzilla.utils;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.ws.commons.util.Base64;
+import org.apache.ws.commons.util.Base64.DecodingException;
+import org.eclipse.lyo.samples.bugzilla.Credentials;
+import org.eclipse.lyo.samples.bugzilla.exception.RestException;
+import org.eclipse.lyo.samples.bugzilla.exception.UnauthroziedException;
+import org.eclipse.lyo.samples.bugzilla.resources.Error;
+
+/**
+ * Utilities for working with HTTP requests and responses.
+ *
+ * @author Samuel Padgett <spadgett@us.ibm.com>
+ */
+public class HttpUtils {
+
+ public static final String AUTHORIZATION_HEADER = "Authorization";
+ public static final String WWW_AUTHENTICATE_HEADER = "WWW-Authenticate";
+ private static final String BASIC_AUTHORIZATION_PREFIX = "Basic ";
+ private static final String WWW_AUTHENTICATE_HEADER_VALUE = BASIC_AUTHORIZATION_PREFIX
+ + "realm=\"Bugzilla\"";
+
+ /**
+ * Gets the credentials from an HTTP request.
+ *
+ * @param request
+ * the request
+ * @return the Bugzilla credentials or <code>null</code> if the request did
+ * not contain an <code>Authorization</code> header
+ * @throws UnauthroziedException
+ * on problems reading the credentials from the
+ * <code>Authorization</code> request header
+ */
+ public static Credentials getCredentials(HttpServletRequest request)
+ throws UnauthroziedException {
+ String authorizationHeader = request.getHeader(AUTHORIZATION_HEADER);
+ if (authorizationHeader == null || "".equals(authorizationHeader)) {
+ return null;
+ }
+
+ Credentials credentials = new Credentials();
+ if (!authorizationHeader.startsWith(HttpUtils.BASIC_AUTHORIZATION_PREFIX)) {
+ throw new UnauthroziedException(
+ "Only basic access authentication is supported.");
+ }
+
+ String encodedString = authorizationHeader.substring(HttpUtils.BASIC_AUTHORIZATION_PREFIX.length());
+ try {
+ String unencodedString = new String(Base64.decode(encodedString), "UTF-8");
+ int seperator = unencodedString.indexOf(':');
+ if (seperator == -1) {
+ throw new UnauthroziedException("Invalid Authorization header value.");
+ }
+
+ credentials.setUsername(unencodedString.substring(0, seperator));
+ credentials.setPassword(unencodedString.substring(seperator + 1));
+ } catch (DecodingException e) {
+ throw new UnauthroziedException("Username and password not Base64 encoded.");
+ } catch (UnsupportedEncodingException e) {
+ throw new UnauthroziedException("Invalid Authorization header value.");
+ }
+
+ return credentials;
+ }
+
+ public static void sendUnauthorizedResponse(HttpServletResponse response,
+ UnauthroziedException e) throws IOException {
+ response.setHeader(WWW_AUTHENTICATE_HEADER,
+ WWW_AUTHENTICATE_HEADER_VALUE);
+ sendErrorResponse(response, e);
+ }
+
+ private static void sendErrorResponse(HttpServletResponse response,
+ RestException e) throws IOException {
+ response.setContentType("application/rdf+xml");
+ RdfUtils.sendErrorResponse(response, Error
+ .fromRestException(e),
+ RdfUtils.JENA_LANG_ABBREVIATED_RDF_XML);
+ }
+}
diff --git a/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/utils/RdfUtils.java b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/utils/RdfUtils.java
index b810c33..f115a5f 100644
--- a/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/utils/RdfUtils.java
+++ b/org.eclipse.lyo.samples.bugzilla/src/main/java/org/eclipse/lyo/samples/bugzilla/utils/RdfUtils.java
@@ -16,16 +16,11 @@
package org.eclipse.lyo.samples.bugzilla.utils;
import java.io.IOException;
-import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
-import jbugz.base.Bug;
-
-import org.eclipse.lyo.samples.bugzilla.resources.BugzillaChangeRequest;
-
import thewebsemantic.Bean2RDF;
import com.hp.hpl.jena.rdf.model.Model;
@@ -64,14 +59,21 @@ public class RdfUtils {
response.flushBuffer();
}
- public static void sendRdfResponse(HttpServletResponse response, Bug bug,
- String lang) throws URISyntaxException, IOException {
+ public static void sendErrorResponse(HttpServletResponse response,
+ org.eclipse.lyo.samples.bugzilla.resources.Error error, String lang)
+ throws IOException {
+ response.setStatus(error.getStatusCode());
+ sendRdfResponse(response, error, lang);
+ }
+
+ public static void sendRdfResponse(HttpServletResponse response,
+ Object resource, String lang) throws IOException {
Model m = createModel();
Bean2RDF writer = new Bean2RDF(m);
- writer.save(BugzillaChangeRequest.fromBug(bug));
+ writer.save(resource);
writeModel(response, m, lang);
}
-
+
/**
* Remove some extra stuff Jenabean puts in the model that we don't want.
*
diff --git a/org.eclipse.lyo.samples.bugzilla/src/test/java/org/eclipse/lyo/samples/bugzilla/test/TestConnection.java b/org.eclipse.lyo.samples.bugzilla/src/test/java/org/eclipse/lyo/samples/bugzilla/test/TestConnection.java
index 26399d7..2c724e2 100644
--- a/org.eclipse.lyo.samples.bugzilla/src/test/java/org/eclipse/lyo/samples/bugzilla/test/TestConnection.java
+++ b/org.eclipse.lyo.samples.bugzilla/src/test/java/org/eclipse/lyo/samples/bugzilla/test/TestConnection.java
@@ -15,9 +15,11 @@
*******************************************************************************/
package org.eclipse.lyo.samples.bugzilla.test;
+import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Properties;
import jbugz.base.Bug;
import jbugz.base.BugzillaConnector;
@@ -25,6 +27,7 @@ import jbugz.rpc.ReportBug;
import junit.framework.TestCase;
import org.eclipse.lyo.samples.bugzilla.BugzillaInitializer;
+import org.eclipse.lyo.samples.bugzilla.Credentials;
import org.eclipse.lyo.samples.bugzilla.jbugzx.base.Product;
import org.eclipse.lyo.samples.bugzilla.jbugzx.rpc.ExtendedBugSearch;
import org.eclipse.lyo.samples.bugzilla.jbugzx.rpc.GetAccessibleProducts;
@@ -33,11 +36,31 @@ import org.eclipse.lyo.samples.bugzilla.jbugzx.rpc.GetProducts;
public class TestConnection extends TestCase {
+
+ private static Credentials credentials;
+ static {
+ Properties props = new Properties();
+ try {
+ props.load(BugzillaInitializer.class.getResourceAsStream("/bugz.properties"));
+ String username = props.getProperty("username");
+ String password = props.getProperty("password");
+ System.out.println("username: " + username);
+ System.out.println("password: " + password);
+
+ credentials = new Credentials();
+ credentials.setUsername(username);
+ credentials.setPassword(password);
+
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
public void testConnection() {
try {
- BugzillaInitializer.getBugzillaConnector();
+ BugzillaInitializer.getBugzillaConnector(credentials);
} catch (Exception e) {
e.printStackTrace();
fail();
@@ -47,7 +70,7 @@ public class TestConnection extends TestCase {
public void testReportBug() {
try {
- BugzillaConnector bc = BugzillaInitializer.getBugzillaConnector();
+ BugzillaConnector bc = BugzillaInitializer.getBugzillaConnector(credentials);
Map<String, Object> bugState = new HashMap<String, Object>();
bugState.put("product", "FakePortal");
@@ -71,7 +94,7 @@ public class TestConnection extends TestCase {
Integer[] productIds = null;
try {
- BugzillaConnector bc = BugzillaInitializer.getBugzillaConnector();
+ BugzillaConnector bc = BugzillaInitializer.getBugzillaConnector(credentials);
GetAccessibleProducts gap = new GetAccessibleProducts();
bc.executeMethod(gap);
productIds = gap.getIds();
@@ -83,7 +106,7 @@ public class TestConnection extends TestCase {
}
try {
- BugzillaConnector bc = BugzillaInitializer.getBugzillaConnector();
+ BugzillaConnector bc = BugzillaInitializer.getBugzillaConnector(credentials);
GetProducts gps = new GetProducts(productIds);
bc.executeMethod(gps);
List<Product> products = gps.getProducts();
@@ -95,7 +118,7 @@ public class TestConnection extends TestCase {
}
try {
- BugzillaConnector bc = BugzillaInitializer.getBugzillaConnector();
+ BugzillaConnector bc = BugzillaInitializer.getBugzillaConnector(credentials);
GetLegalValues glv = new GetLegalValues("op_sys", -1);
bc.executeMethod(glv);
String[] values = glv.getValues();
@@ -110,7 +133,7 @@ public class TestConnection extends TestCase {
public void testSearchBugs() {
try {
- BugzillaConnector bc = BugzillaInitializer.getBugzillaConnector();
+ BugzillaConnector bc = BugzillaInitializer.getBugzillaConnector(credentials);
ExtendedBugSearch search = new ExtendedBugSearch(ExtendedBugSearch.ExtendedSearchLimiter.PRODUCT, "FakePortal");
bc.executeMethod(search);