1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
/*******************************************************************************
* 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.ats.rest.internal.util;
import java.net.URI;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriBuilder;
import org.eclipse.osee.ats.api.IAtsWorkItem;
import org.eclipse.osee.ats.rest.IAtsServer;
import org.eclipse.osee.ats.rest.internal.AtsApplication;
import org.eclipse.osee.framework.jdk.core.type.ViewModel;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
/**
* @author Donald G. Dunne
*/
public class RestUtil {
/**
* @return Contents of resource starting at path org.eclipse.osee.ats.rest/OSEE-INF
*/
public static String getResource(String path) throws Exception {
Bundle bundle = FrameworkUtil.getBundle(AtsApplication.class);
URL url = bundle.getEntry("OSEE-INF/" + path);
return Lib.inputStreamToString(url.openStream());
}
public static String simplePageHtml(String title, String message) throws Exception {
String html = getResource("templates/simple.html");
html = html.replaceFirst("<\\?PUT_MESSAGE_HERE\\?>", message);
html = html.replaceFirst("<\\?PUT_TITLE_HERE\\?>", title);
return html;
}
public static String simplePageHtml(String message) throws Exception {
return simplePageHtml("ATS", message);
}
public static Response simplePageResponse(String message) throws Exception {
return simplePageResponse("ATS", message);
}
public static Response simplePageResponse(String title, String message) throws Exception {
return Response.status(200).entity(simplePageHtml(title, message)).build();
}
public static ViewModel simplePage(String message) {
return simplePage("ATS", message);
}
public static ViewModel simplePage(String title, String message) {
return new ViewModel("simple.html") //
.param("PUT_MESSAGE_HERE", message) //
.param("PUT_TITLE_HERE", title);
}
public static Response redirect(IAtsWorkItem workItem, String defaultUrl, IAtsServer atsServer) {
return redirect(Arrays.asList(workItem), defaultUrl, atsServer);
}
public static Response redirect(Collection<? extends IAtsWorkItem> workItems, String defaultUrl, IAtsServer atsServer) {
String actionUrl = getBaseActionUiUrl(defaultUrl, atsServer);
String uuids = "";
for (IAtsWorkItem teamWf : workItems) {
uuids += teamWf.getAtsId() + ",";
}
uuids = uuids.replaceFirst(",$", "");
actionUrl = actionUrl.replaceFirst("UUID", uuids);
URI uri = UriBuilder.fromUri(actionUrl).build();
return Response.seeOther(uri).build();
}
public static String getActionUrl(IAtsWorkItem workItem, String defaultUrl, IAtsServer atsServer) {
return getActionUrl(workItem.getAtsId(), defaultUrl, atsServer);
}
public static String getActionUrl(String atsId, String defaultUrl, IAtsServer atsServer) {
String actionUrl = getBaseActionUiUrl(defaultUrl, atsServer);
actionUrl = actionUrl.replaceFirst("UUID", atsId);
return actionUrl;
}
private static String getBaseActionUiUrl(String defaultUrl, IAtsServer atsServer) {
String actionUrl = atsServer.getConfigValue("ActionUrl2");
if (!Strings.isValid(actionUrl)) {
actionUrl = defaultUrl;
}
return actionUrl;
}
public static Response returnBadRequest(String message) {
return Response.status(Status.BAD_REQUEST).entity(message).build();
}
public static Response returnInternalServerError(String message) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(message).build();
}
}
|