Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public IProject getProject() {
@Override
public void setProject(IProject project) {
this.project = project;
}

@Override
public String getLoginPointName() {
return "Project " + project.getID();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public interface IConnector {
IContainer getContainerForRequirement(Requirement requirement) throws SpecmateException;

/**
* Returns a set of projects given credentials have access to. Returns an empty set if
* credentials are invalid.
* Returns a set of projects given credentials have access to. Returns an empty
* set if credentials are invalid.
*
* @return Returns a set of projects the credentials can access. If successful,
* this set must contain at least the current project which the user
Expand All @@ -71,7 +71,22 @@ public interface IConnector {
* credentials do not match (for any project) an empty set must be
* returned.
*/
Set<IProject> authenticate(String username, String password)
throws SpecmateException;
Set<IProject> authenticate(String username, String password) throws SpecmateException;

/**
* Generates a readable ID of this login point name. This feature is helpful for
* connectors which supports multi-project logins.
*
* For example, a jira connector (which supports multi-project logins) may
* generate a string like 'JIRA Server https://jira.company.com'. Therefore, all
* other connectors which connect to the same server can be identified folded
* toghether.
*
* Connector with identical login point names will listed as just one entry in
* the login dialog.
*
* If your connector does not support multi-project logins, just generate a
* unique name, for example, embedding your project name. 'Project ABC'.
*/
String getLoginPointName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public interface IProjectService {
IProject getProject(String projectName);
Set<String> getProjectNames();
Map<String, IProject> getProjects();
Map<String, String> getLoginPoints();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;

import com.specmate.connectors.api.IConnector;
import com.specmate.connectors.api.IProject;
import com.specmate.connectors.api.IProjectService;

Expand Down Expand Up @@ -41,4 +42,26 @@ public void removeProject(IProject project) {
public Map<String, IProject> getProjects() {
return Collections.unmodifiableMap(projects);
}

@Override
public Map<String, String> getLoginPoints() {

Map<String, String> loginPoints = new HashMap<>();

for (Map.Entry<String, IProject> projectEntries : projects.entrySet()) {

IConnector connector = projectEntries.getValue().getConnector();
if (connector == null) {
continue;
}

String loginPointName = connector.getLoginPointName();

if (!loginPoints.containsKey(loginPointName)) {
loginPoints.put(loginPointName, projectEntries.getKey());
}
}

return loginPoints;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.specmate.emfjson;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
Expand Down Expand Up @@ -77,10 +79,9 @@ public boolean serializeContainedElements(EObject object) {
/**
* constructor
*
* @param uriFactory
* The IURIFactory that is used for generating URIs from EObjects
* @param stopPredicate
* The stop predicate to indicate where to stop serializing
* @param uriFactory The IURIFactory that is used for generating URIs from
* EObjects
* @param stopPredicate The stop predicate to indicate where to stop serializing
*/
public EMFJsonSerializer(IURIFactory uriFactory, ISerializationConfiguration config) {
this.uriFactory = uriFactory;
Expand All @@ -90,8 +91,7 @@ public EMFJsonSerializer(IURIFactory uriFactory, ISerializationConfiguration con
/**
* Serializes an {@link EObject} to JSON
*
* @param eObject
* The {@link EObject} to serialize
* @param eObject The {@link EObject} to serialize
* @return The JSON representation of <code>object</code>
* @throws JSONException
* @throws SpecmateException
Expand All @@ -107,11 +107,9 @@ public JSONObject serialize(EObject eObject) throws JSONException, SpecmateExcep
/**
* Serializes a list of {@link EObject} to JSON
*
* @param list
* The list of {@link EObject}s to serialize
* @param list The list of {@link EObject}s to serialize
* @return The JSON representation of <code>list</code>
* @throws SpecmateException
* If the object cannot be serialized
* @throws SpecmateException If the object cannot be serialized
*/
public JSONArray serialize(List<?> list) throws JSONException, SpecmateException {
try {
Expand All @@ -121,13 +119,27 @@ public JSONArray serialize(List<?> list) throws JSONException, SpecmateException
}
}

/**
* Serializes a list of {@link EObject} to JSON
*
* @param list The list of {@link EObject}s to serialize
* @return The JSON representation of <code>list</code>
* @throws SpecmateException If the object cannot be serialized
*/
public JSONObject serialize(Map<?, ?> map) throws JSONException, SpecmateException {
try {
return serializeMap(map);
} catch (Exception e) {
throw new SpecmateInternalException(ErrorCode.SERALIZATION, e);
}
}

/**
* Serializes an {@link EObject} to JSON at certain serializing depth. Stops
* serializing if indicated by {@link ISerializerStopPredicate.stopAtDepth} from
* the currently set stop predicate.
*
* @param eObject
* The {@link EObject} to serialize
* @param eObject The {@link EObject} to serialize
* @return The JSON representation of <code>eObject</code>
* @throws SpecmateException
*/
Expand All @@ -144,10 +156,9 @@ private JSONObject serializeObject(EObject eObject) throws SpecmateException {
* Serializes the type informations (namespace URI and class name) of eObject to
* the given {@link JSONObject}.
*
* @param eObject
* The {@link EObject} for which to serialize the type information
* @param jsonObj
* The JSON object where to put the serialized type information.
* @param eObject The {@link EObject} for which to serialize the type
* information
* @param jsonObj The JSON object where to put the serialized type information.
* @throws JSONException
*/
private void serializeType(EObject eObject, JSONObject jsonObj) throws JSONException {
Expand All @@ -161,10 +172,8 @@ private void serializeType(EObject eObject, JSONObject jsonObj) throws JSONExcep
/**
* Serializes the URI of eObject into the given {@link JSONObject}.
*
* @param eObject
* The {@link EObject} of which to serialize the URI
* @param jsonObj
* The {@link JSONObject} where to put the serialized URI
* @param eObject The {@link EObject} of which to serialize the URI
* @param jsonObj The {@link JSONObject} where to put the serialized URI
* @throws SpecmateException
*/
private void serializeUri(EObject eObject, JSONObject jsonObj) throws SpecmateException {
Expand All @@ -175,14 +184,15 @@ private void serializeUri(EObject eObject, JSONObject jsonObj) throws SpecmateEx
* Serializes a value. EObjects and Lists are handled recursively, any other
* type of object is serialized as String.
*
* @param value
* The value to serialize
* @param value The value to serialize
* @return The JSON representation of <code>value</code>
* @throws SpecmateException
*/
private Object serializeValue(Object value) throws SpecmateException {
if (value instanceof EList) {
return serializeList((EList<?>) value);
} else if (value instanceof Map<?, ?>) {
return serializeMap((Map<?, ?>) value);
} else if (value instanceof EObject) {
return serializeObject((EObject) value);
} else if (value instanceof Double) {
Expand All @@ -197,8 +207,7 @@ private Object serializeValue(Object value) throws SpecmateException {
/**
* Serializes a list of objects
*
* @param list
* The list of objects to serialize
* @param list The list of objects to serialize
* @return A {@link JSONArray} containing the JSON representation of all members
* of <code>list</code>
* @throws SpecmateException
Expand All @@ -211,13 +220,28 @@ private JSONArray serializeList(List<?> list) throws SpecmateException {
return array;
}

/**
* Serializes a list of objects
*
* @param map The map of objects to serialize
* @return A {@link JSONObject} containing the JSON representation of all
* members of <code>map</code>
* @throws SpecmateException
*/
private JSONObject serializeMap(Map<?, ?> map) throws SpecmateException {
JSONObject jsonobject = new JSONObject();
for (Entry<?, ?> entry : map.entrySet()) {
jsonobject.put((String) serializeValue(entry.getKey()), serializeValue(entry.getValue()));
}
return jsonobject;
}

/**
* Serializes an EObject or a List as proxy JSON structures. That means
* value<code>value</code> is not completely serialized but only the URI
* inforamtion.
*
* @param value
* The value to serialize as proxy
* @param value The value to serialize as proxy
* @return A JSON proxy structure
* @throws SpecmateException
*/
Expand All @@ -241,8 +265,7 @@ private Object serializeProxy(Object value) throws SpecmateException {
* Transforms an {@link EObject} into a JSON proxy structure, i.e. a
* {@link JSONObject} that contains the uri of the {@link EObject}.
*
* @param eObject
* The {@link EObject} for which to obtain a proxy
* @param eObject The {@link EObject} for which to obtain a proxy
* @return The JSON proxy structure
* @throws SpecmateException
*/
Expand All @@ -258,10 +281,8 @@ public Object getProxy(EObject eObject) throws SpecmateException {
* Serializes all feature of an {@link EObject} into the JSON object
* <code>jsonObj</code>. For references the method obtains proxies.
*
* @param eObject
* The {@link EObject} for which to serialize all features
* @param jsonObj
* The JSON object where to put all serialization results
* @param eObject The {@link EObject} for which to serialize all features
* @param jsonObj The JSON object where to put all serialization results
* @throws JSONException
* @throws SpecmateException
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.specmate.emfrest.authentication;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

import org.eclipse.emf.ecore.resource.Resource;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import com.specmate.connectors.api.IProjectService;
import com.specmate.emfrest.api.IRestService;
import com.specmate.emfrest.api.RestServiceBase;
import com.specmate.rest.RestResult;

@Component(service = IRestService.class)
public class LoginPoints extends RestServiceBase {
public static final String SERVICE_NAME = "loginpoints";
private IProjectService projectService;

@Override
public String getServiceName() {
return SERVICE_NAME;
}

@Override
public boolean canGet(Object target) {
return (target instanceof Resource);
}

@Override
public RestResult<?> get(Object object, MultivaluedMap<String, String> queryParams, String token) {

Map<String, String> loginPoints = projectService.getLoginPoints();

// GenericEntity entity = new GenericEntity<Map<String, String>>(loginPoints){};
// return new RestResult<>(Response.Status.OK, entity);

return new RestResult<>(Response.Status.OK, loginPoints );
}

@Reference
public void setProjectService(IProjectService projectService) {
this.projectService = projectService;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
import com.specmate.emfrest.internal.metrics.MetricsDynamicFeature;
import com.specmate.emfrest.internal.rest.JsonEObjectWriter;
import com.specmate.emfrest.internal.rest.JsonListWriter;
import com.specmate.emfrest.internal.rest.JsonMapWriter;
import com.specmate.emfrest.internal.rest.JsonReader;
import com.specmate.emfrest.internal.rest.RootResource;

class EmfRestJerseyApplication extends ResourceConfig {

public EmfRestJerseyApplication() {
registerClasses(RootResource.class, JsonEObjectWriter.class, JsonListWriter.class, JsonReader.class,
AuthenticationFilter.class, MetricsDynamicFeature.class);
registerClasses(RootResource.class, JsonEObjectWriter.class, JsonListWriter.class, JsonMapWriter.class,
JsonReader.class, AuthenticationFilter.class, MetricsDynamicFeature.class);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.specmate.auth.api.IAuthenticationService;
import com.specmate.common.exception.SpecmateException;
import com.specmate.emfrest.authentication.Login;
import com.specmate.emfrest.authentication.LoginPoints;
import com.specmate.emfrest.authentication.Logout;
import com.specmate.emfrest.authentication.ProjectNames;
import com.specmate.model.administration.AdministrationFactory;
Expand All @@ -32,6 +33,7 @@ public class AuthenticationFilter implements ContainerRequestFilter {
private final String HEARTBEAT_PARAMETER = "heartbeat";
private final String REST_URL = ".+services/rest/";
private Pattern loginPattern = Pattern.compile(REST_URL + Login.SERVICE_NAME);
private Pattern loginPointsPattern = Pattern.compile(REST_URL + LoginPoints.SERVICE_NAME);
private Pattern logoutPattern = Pattern.compile(REST_URL + Logout.SERVICE_NAME);
private Pattern projectNamesPattern = Pattern.compile(REST_URL + ProjectNames.SERVICE_NAME);
private Pattern reindexPattern = Pattern.compile(REST_URL + REINDEX_SERVICE_NAME);
Expand All @@ -54,8 +56,8 @@ public void filter(ContainerRequestContext requestContext) throws IOException {

// Validate the Authorization header
if (!AuthorizationHeader.isAuthenticationSet(requestContext)) {
logService.log(LogService.LOG_INFO, "No credentials set: on path "
+ requestContext.getUriInfo().getAbsolutePath().toString());
logService.log(LogService.LOG_INFO,
"No credentials set: on path " + requestContext.getUriInfo().getAbsolutePath().toString());
abortWithUnauthorized(requestContext);
return;
}
Expand Down Expand Up @@ -104,10 +106,11 @@ private void abortWithUnauthorized(ContainerRequestContext requestContext) {
private boolean isNotSecured(ContainerRequestContext requestContext) {
String path = requestContext.getUriInfo().getAbsolutePath().toString();
Matcher matcherLogin = loginPattern.matcher(path);
Matcher matcherLoginPoints = loginPointsPattern.matcher(path);
Matcher matcherLogout = logoutPattern.matcher(path);
Matcher matcherProjectNames = projectNamesPattern.matcher(path);
Matcher matcherReindex = reindexPattern.matcher(path);
return matcherLogin.matches() || matcherLogout.matches() || matcherProjectNames.matches()
|| matcherReindex.matches();
return matcherLogin.matches() || matcherLoginPoints.matches() || matcherLogout.matches()
|| matcherProjectNames.matches() || matcherReindex.matches();
}
}
Loading