Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ViewModel.java41
-rw-r--r--plugins/org.eclipse.osee.jaxrs.server/META-INF/MANIFEST.MF1
-rw-r--r--plugins/org.eclipse.osee.jaxrs.server/OSGI-INF/jaxrs.mvc.writer.xml9
-rw-r--r--plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/mvc/ViewModelWriter.java185
-rw-r--r--plugins/org.eclipse.osee.jaxrs/META-INF/MANIFEST.MF7
-rw-r--r--plugins/org.eclipse.osee.jaxrs/src/org/eclipse/osee/jaxrs/mvc/AbstractViewResolver.java42
-rw-r--r--plugins/org.eclipse.osee.jaxrs/src/org/eclipse/osee/jaxrs/mvc/JaxRsMvcUtils.java56
-rw-r--r--plugins/org.eclipse.osee.jaxrs/src/org/eclipse/osee/jaxrs/mvc/ViewResolver.java31
-rw-r--r--plugins/org.eclipse.osee.jaxrs/src/org/eclipse/osee/jaxrs/mvc/ViewWriter.java27
9 files changed, 397 insertions, 2 deletions
diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ViewModel.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ViewModel.java
new file mode 100644
index 00000000000..95ac987962f
--- /dev/null
+++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ViewModel.java
@@ -0,0 +1,41 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Boeing.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Boeing - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.osee.framework.jdk.core.type;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * @author Roberto E. Escobar
+ */
+public class ViewModel {
+
+ private final String viewId;
+ private final Map<String, Object> model = new LinkedHashMap<String, Object>();
+
+ public ViewModel(String viewId) {
+ this.viewId = viewId;
+ }
+
+ public String getViewId() {
+ return viewId;
+ }
+
+ public Map<String, Object> asMap() {
+ return model;
+ }
+
+ public ViewModel param(String key, Object value) {
+ model.put(key, value);
+ return this;
+ }
+
+}
diff --git a/plugins/org.eclipse.osee.jaxrs.server/META-INF/MANIFEST.MF b/plugins/org.eclipse.osee.jaxrs.server/META-INF/MANIFEST.MF
index db7977f679a..651a6283804 100644
--- a/plugins/org.eclipse.osee.jaxrs.server/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.osee.jaxrs.server/META-INF/MANIFEST.MF
@@ -61,6 +61,7 @@ Import-Package: com.google.common.cache,
org.eclipse.osee.framework.jdk.core.util,
org.eclipse.osee.jaxrs,
org.eclipse.osee.jaxrs.client,
+ org.eclipse.osee.jaxrs.mvc,
org.eclipse.osee.logger,
org.eclipse.osee.template.engine,
org.osgi.framework,
diff --git a/plugins/org.eclipse.osee.jaxrs.server/OSGI-INF/jaxrs.mvc.writer.xml b/plugins/org.eclipse.osee.jaxrs.server/OSGI-INF/jaxrs.mvc.writer.xml
new file mode 100644
index 00000000000..243994a11ff
--- /dev/null
+++ b/plugins/org.eclipse.osee.jaxrs.server/OSGI-INF/jaxrs.mvc.writer.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0">
+ <implementation class="org.eclipse.osee.jaxrs.server.internal.mvc.ViewModelWriter" />
+ <service>
+ <provide interface="java.lang.Object"/>
+ <provide interface="javax.ws.rs.ext.MessageBodyWriter"/>
+ </service>
+ <reference bind="addResolver" unbind="removeResolver" cardinality="1..n" interface="org.eclipse.osee.jaxrs.mvc.ViewResolver" name="ViewResolver" policy="dynamic"/>
+</scr:component>
diff --git a/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/mvc/ViewModelWriter.java b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/mvc/ViewModelWriter.java
new file mode 100644
index 00000000000..72b4dc43dfc
--- /dev/null
+++ b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/mvc/ViewModelWriter.java
@@ -0,0 +1,185 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Boeing.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Boeing - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.osee.jaxrs.server.internal.mvc;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
+import javax.ws.rs.Produces;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.container.ResourceInfo;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response.Status;
+import javax.ws.rs.ext.MessageBodyWriter;
+import javax.ws.rs.ext.Provider;
+import org.eclipse.osee.framework.jdk.core.type.ViewModel;
+import org.eclipse.osee.jaxrs.OseeWebApplicationException;
+import org.eclipse.osee.jaxrs.mvc.ViewResolver;
+import org.eclipse.osee.jaxrs.mvc.ViewWriter;
+import org.eclipse.osee.jaxrs.server.internal.JaxRsUtils;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.ServiceReference;
+
+/**
+ * @author Roberto E. Escobar
+ */
+@Provider
+public class ViewModelWriter implements MessageBodyWriter<ViewModel> {
+
+ private final ConcurrentHashMap<String, ViewResolver<?>> resolvers =
+ new ConcurrentHashMap<String, ViewResolver<?>>();
+
+ @Context
+ private ResourceInfo resourceInfo;
+
+ public void addResolver(ServiceReference<ViewResolver<?>> reference) {
+ String componentName = JaxRsUtils.getComponentName(reference);
+ Bundle bundle = reference.getBundle();
+ ViewResolver<?> resolver = bundle.getBundleContext().getService(reference);
+ resolvers.put(componentName, resolver);
+ }
+
+ public void removeResolver(ServiceReference<ViewResolver<?>> reference) {
+ String componentName = JaxRsUtils.getComponentName(reference);
+ resolvers.remove(componentName);
+ }
+
+ private Iterable<ViewResolver<?>> getViewResolvers() {
+ return resolvers.values();
+ }
+
+ @Override
+ public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
+ return ViewModel.class.isAssignableFrom(type);
+ }
+
+ @Override
+ public long getSize(ViewModel model, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
+ return -1;
+ }
+
+ @Override
+ public void writeTo(ViewModel model, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
+ ViewWriter writer = resolve(model, mediaType);
+ if (writer == null) {
+ throw new OseeWebApplicationException(Status.NOT_FOUND, "Template [%s] could not be resolved",
+ model.getViewId());
+ }
+ httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, writer.getMediaType());
+ writer.write(httpHeaders, entityStream);
+ }
+
+ private ViewWriter resolve(ViewModel model, MediaType mediaType) {
+ ViewWriter toReturn = null;
+ if (model instanceof ViewWriter) {
+ toReturn = (ViewWriter) model;
+ } else {
+ Class<?> resourceClass = resourceInfo.getResourceClass();
+ Iterable<ViewResolver<?>> resolvers = getViewResolvers();
+ List<MediaType> mediaTypes = getMediaTypesProduced(resourceInfo);
+ toReturn = resolve(model, resourceClass, mediaTypes, resolvers);
+ }
+ return toReturn;
+ }
+
+ private ViewWriter resolve(ViewModel model, Class<?> resolvingClass, Iterable<MediaType> mediaTypes, Iterable<ViewResolver<?>> resolvers) {
+ ViewWriter toReturn = null;
+ for (ViewResolver<?> resolver : resolvers) {
+ for (MediaType mediaType : mediaTypes) {
+ ViewWriter writer = resolve(model, resolvingClass, mediaType, resolver);
+ if (writer != null) {
+ toReturn = writer;
+ break;
+ }
+ }
+ }
+ return toReturn;
+ }
+
+ private <T> ResolvedView<T> resolve(ViewModel model, Class<?> resourceClass, MediaType mediaType, ViewResolver<T> resolver) {
+ ResolvedView<T> toReturn = null;
+ T viewReference = resolver.resolve(model.getViewId(), mediaType);
+ if (viewReference != null) {
+ toReturn = newResolved(resolver, resourceClass, mediaType, model, viewReference);
+ }
+ return toReturn;
+ }
+
+ private static <T> List<T> getAnnotations(Method method, Class<T> clazz) {
+ List<T> annotations = new ArrayList<T>();
+ if (method != null) {
+ for (Annotation annotation : method.getAnnotations()) {
+ if (annotation.annotationType().isAssignableFrom(clazz)) {
+ @SuppressWarnings("unchecked")
+ T object = (T) annotation;
+ annotations.add(object);
+ }
+ }
+ }
+ return annotations;
+ }
+
+ private static List<MediaType> getMediaTypesProduced(ResourceInfo resourceInfo) {
+ List<MediaType> produces = new ArrayList<MediaType>();
+
+ Method method = resourceInfo.getResourceMethod();
+ List<Produces> annotations = getAnnotations(method, Produces.class);
+ for (Produces annotation : annotations) {
+ String[] mediaTypes = annotation.value();
+ for (String mediaType : mediaTypes) {
+ produces.add(MediaType.valueOf(mediaType));
+ }
+ }
+ if (produces.isEmpty()) {
+ produces.add(MediaType.WILDCARD_TYPE);
+ }
+ return produces;
+ }
+
+ private static <T> ResolvedView<T> newResolved(ViewResolver<T> resolver, Class<?> resourceClass, MediaType mediaType, ViewModel model, T viewReference) {
+ String viewId = model.getViewId();
+ ResolvedView<T> toReturn = new ResolvedView<T>(viewId, resolver, mediaType, viewReference);
+ toReturn.asMap().putAll(model.asMap());
+ return toReturn;
+ }
+
+ private static final class ResolvedView<T> extends ViewModel implements ViewWriter {
+
+ private final ViewResolver<T> resolver;
+ private final MediaType mediaType;
+ private final T viewReference;
+
+ public ResolvedView(String viewId, ViewResolver<T> resolver, MediaType mediaType, T viewReference) {
+ super(viewId);
+ this.resolver = resolver;
+ this.mediaType = mediaType;
+ this.viewReference = viewReference;
+ }
+
+ @Override
+ public MediaType getMediaType() {
+ return mediaType;
+ }
+
+ @Override
+ public void write(MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
+ resolver.write(this, viewReference, mediaType, httpHeaders, entityStream);
+ }
+ }
+} \ No newline at end of file
diff --git a/plugins/org.eclipse.osee.jaxrs/META-INF/MANIFEST.MF b/plugins/org.eclipse.osee.jaxrs/META-INF/MANIFEST.MF
index f610e71e4c4..9a833b66e50 100644
--- a/plugins/org.eclipse.osee.jaxrs/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.osee.jaxrs/META-INF/MANIFEST.MF
@@ -6,11 +6,14 @@ Bundle-Version: 0.19.0.qualifier
Bundle-Vendor: Eclipse Open System Engineering Environment
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ActivationPolicy: lazy
-Import-Package: javax.ws.rs,
+Import-Package: javax.servlet;version="2.6.0",
+ javax.ws.rs,
javax.ws.rs.core,
javax.ws.rs.ext,
+ org.eclipse.osee.framework.jdk.core.type,
org.eclipse.osee.framework.jdk.core.util
-Export-Package: org.eclipse.osee.jaxrs
+Export-Package: org.eclipse.osee.jaxrs,
+ org.eclipse.osee.jaxrs.mvc
Require-Bundle: org.codehaus.jackson.core;bundle-version="1.9.13",
org.codehaus.jackson.jaxrs;bundle-version="1.9.13",
org.codehaus.jackson.mapper;bundle-version="1.9.13",
diff --git a/plugins/org.eclipse.osee.jaxrs/src/org/eclipse/osee/jaxrs/mvc/AbstractViewResolver.java b/plugins/org.eclipse.osee.jaxrs/src/org/eclipse/osee/jaxrs/mvc/AbstractViewResolver.java
new file mode 100644
index 00000000000..3cc6dfad672
--- /dev/null
+++ b/plugins/org.eclipse.osee.jaxrs/src/org/eclipse/osee/jaxrs/mvc/AbstractViewResolver.java
@@ -0,0 +1,42 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Boeing.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Boeing - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.osee.jaxrs.mvc;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.charset.Charset;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import org.eclipse.osee.framework.jdk.core.type.ViewModel;
+
+/**
+ * @author Roberto E. Escobar
+ */
+public abstract class AbstractViewResolver<T> implements ViewResolver<T> {
+
+ protected Charset getDefaultEncoding() {
+ return Charset.forName("UTF-8");
+ }
+
+ protected Charset computeEncoding(MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, Charset defaultEncoding) {
+ return JaxRsMvcUtils.computeEncoding(mediaType, httpHeaders, defaultEncoding);
+ }
+
+ @Override
+ public final void write(ViewModel model, T view, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream output) throws IOException {
+ Charset defaultEncoding = getDefaultEncoding();
+ Charset encoding = computeEncoding(mediaType, httpHeaders, defaultEncoding);
+ write(model, view, mediaType, httpHeaders, output, encoding);
+ }
+
+ public abstract void write(ViewModel model, T view, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream output, Charset encoding) throws IOException;
+
+} \ No newline at end of file
diff --git a/plugins/org.eclipse.osee.jaxrs/src/org/eclipse/osee/jaxrs/mvc/JaxRsMvcUtils.java b/plugins/org.eclipse.osee.jaxrs/src/org/eclipse/osee/jaxrs/mvc/JaxRsMvcUtils.java
new file mode 100644
index 00000000000..0bc0bc25b3f
--- /dev/null
+++ b/plugins/org.eclipse.osee.jaxrs/src/org/eclipse/osee/jaxrs/mvc/JaxRsMvcUtils.java
@@ -0,0 +1,56 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Boeing.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Boeing - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.osee.jaxrs.mvc;
+
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+
+/**
+ * @author Roberto E. Escobar
+ */
+public final class JaxRsMvcUtils {
+
+ private JaxRsMvcUtils() {
+ // Utility class
+ }
+
+ public static Charset UTF_8_ENCODING = newCharset("UTF-8");
+
+ private static Charset newCharset(String value) {
+ return Charset.forName(value);
+ }
+
+ public static Charset computeEncoding(MediaType mediaType, Map<String, List<Object>> httpHeaders) {
+ return computeEncoding(mediaType, httpHeaders, null);
+ }
+
+ public static Charset computeEncoding(MediaType mediaType, Map<String, List<Object>> httpHeaders, Charset defaultEncoding) {
+ String charset = mediaType.getParameters().get(MediaType.CHARSET_PARAMETER);
+ Charset encoding = defaultEncoding != null ? defaultEncoding : UTF_8_ENCODING;
+ MediaType contentType = mediaType;
+ if (charset != null) {
+ encoding = Charset.forName(charset);
+ } else {
+ HashMap<String, String> params = new HashMap<String, String>(mediaType.getParameters());
+ params.put(MediaType.CHARSET_PARAMETER, encoding.name());
+ contentType = new MediaType(mediaType.getType(), mediaType.getSubtype(), params);
+ }
+ ArrayList<Object> typeList = new ArrayList<Object>(1);
+ typeList.add(contentType.toString());
+ httpHeaders.put(HttpHeaders.CONTENT_TYPE, typeList);
+ return encoding;
+ }
+}
diff --git a/plugins/org.eclipse.osee.jaxrs/src/org/eclipse/osee/jaxrs/mvc/ViewResolver.java b/plugins/org.eclipse.osee.jaxrs/src/org/eclipse/osee/jaxrs/mvc/ViewResolver.java
new file mode 100644
index 00000000000..03bb7b90e58
--- /dev/null
+++ b/plugins/org.eclipse.osee.jaxrs/src/org/eclipse/osee/jaxrs/mvc/ViewResolver.java
@@ -0,0 +1,31 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Boeing.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Boeing - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.osee.jaxrs.mvc;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.Provider;
+import org.eclipse.osee.framework.jdk.core.type.ViewModel;
+
+/**
+ * @author Roberto E. Escobar
+ */
+@Provider
+public interface ViewResolver<T> {
+
+ T resolve(String viewId, MediaType mediaType);
+
+ void write(ViewModel model, T view, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException;
+
+} \ No newline at end of file
diff --git a/plugins/org.eclipse.osee.jaxrs/src/org/eclipse/osee/jaxrs/mvc/ViewWriter.java b/plugins/org.eclipse.osee.jaxrs/src/org/eclipse/osee/jaxrs/mvc/ViewWriter.java
new file mode 100644
index 00000000000..8c81511ce1c
--- /dev/null
+++ b/plugins/org.eclipse.osee.jaxrs/src/org/eclipse/osee/jaxrs/mvc/ViewWriter.java
@@ -0,0 +1,27 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Boeing.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Boeing - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.osee.jaxrs.mvc;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+
+/**
+ * @author Roberto E. Escobar
+ */
+public interface ViewWriter {
+
+ MediaType getMediaType();
+
+ void write(MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException;
+
+} \ No newline at end of file

Back to the top