3
3
import com .hp .hpl .jena .graph .Node ;
4
4
import com .hp .hpl .jena .graph .NodeFactory ;
5
5
import com .hp .hpl .jena .graph .Triple ;
6
- import com .hp .hpl .jena .query .*;
6
+ import com .hp .hpl .jena .query .QuerySolution ;
7
+ import com .hp .hpl .jena .query .ResultSet ;
8
+ import com .hp .hpl .jena .query .ResultSetFormatter ;
7
9
import com .hp .hpl .jena .rdf .model .Model ;
8
10
import com .hp .hpl .jena .rdf .model .RDFNode ;
9
11
import com .hp .hpl .jena .sparql .core .DatasetGraph ;
25
27
import org .json .simple .JSONObject ;
26
28
import org .slf4j .Logger ;
27
29
import org .slf4j .LoggerFactory ;
28
- import uk .ac .open .kmi .basil .sparql .QueryParameter ;
30
+ import uk .ac .open .kmi .basil .core .InvocationResult ;
31
+ import uk .ac .open .kmi .basil .core .exceptions .SpecificationParsingException ;
29
32
import uk .ac .open .kmi .basil .sparql .Specification ;
30
- import uk .ac .open .kmi .basil .sparql .VariablesBinder ;
31
- import uk .ac .open .kmi .basil .store .Store ;
32
33
import uk .ac .open .kmi .basil .view .Items ;
33
34
import uk .ac .open .kmi .basil .view .View ;
34
35
import uk .ac .open .kmi .basil .view .Views ;
39
40
import javax .ws .rs .core .Response ;
40
41
import javax .ws .rs .core .Response .ResponseBuilder ;
41
42
import javax .ws .rs .core .Variant ;
43
+ import java .io .IOException ;
42
44
import java .io .StringWriter ;
43
45
import java .io .UnsupportedEncodingException ;
44
46
import java .net .HttpURLConnection ;
45
47
import java .net .URI ;
46
- import java .util .*;
48
+ import java .util .Arrays ;
49
+ import java .util .Iterator ;
50
+ import java .util .List ;
51
+ import java .util .Map ;
47
52
48
53
@ Path ("{id}" )
49
54
@ Api (value = "/basil" , description = "BASIL operations" )
@@ -55,45 +60,7 @@ public class ApiResource extends AbstractResource {
55
60
private Response performQuery (String id ,
56
61
MultivaluedMap <String , String > parameters , String extension ) {
57
62
try {
58
- if (!isValidId (id )) {
59
- return Response .status (400 ).build ();
60
- }
61
- if (!extension .equals ("" ) && !isValidExtension (extension )) {
62
- return Response .status (400 ).build ();
63
- }
64
- Store store = getDataStore ();
65
- if (!store .existsSpec (id )) {
66
- return Response .status (404 ).build ();
67
- }
68
-
69
- Specification specification = store .loadSpec (id );
70
- VariablesBinder binder = new VariablesBinder (specification );
71
-
72
- List <String > missing = new ArrayList <String >();
73
- for (QueryParameter qp : specification .getParameters ()) {
74
- if (parameters .containsKey (qp .getName ())) {
75
- List <String > values = parameters .get (qp .getName ());
76
- binder .bind (qp .getName (), values .get (0 ));
77
- } else if (!qp .isOptional ()) {
78
- missing .add (qp .getName ());
79
- }
80
- }
81
-
82
- if (!missing .isEmpty ()) {
83
- StringBuilder ms = new StringBuilder ();
84
- ms .append ("Missing mandatory query parameters: " );
85
- for (String p : missing ) {
86
- ms .append (p );
87
- ms .append ("\t " );
88
- }
89
- ms .append ("\n " );
90
- return Response .status (400 ).entity (ms .toString ()).build ();
91
- }
92
-
93
- Query q = binder .toQuery ();
94
- QueryExecution qe = QueryExecutionFactory .sparqlService (
95
- specification .getEndpoint (), q );
96
- Object entity = null ;
63
+ InvocationResult r = getApiManager ().invokeApi (id , parameters );
97
64
98
65
MediaType type = null ;
99
66
// If we have an extension
@@ -104,24 +71,19 @@ private Response performQuery(String id,
104
71
105
72
// No extension, check if the extension is the name of a view
106
73
if (type == null ) {
107
- Views views = store . loadViews (id );
74
+ Views views = getApiManager (). listViews (id );
108
75
if (views .exists (extension )) {
109
76
View view = views .byName (extension );
110
77
StringWriter writer = new StringWriter ();
111
78
Items data = null ;
112
- if (q .isSelectType ()) {
113
- data = Items .create (qe .execSelect ());
114
- } else if (q .isConstructType ()) {
115
- data = Items .create (qe .execConstructTriples ());
116
- } else if (q .isAskType ()) {
117
- data = Items .create (qe .execAsk ());
118
- } else if (q .isDescribeType ()) {
119
- data = Items .create (qe .execDescribeTriples ());
120
- } else {
121
- return Response
122
- .serverError ()
123
- .entity ("Unsupported query type: "
124
- + q .getQueryType ()).build ();
79
+ if (r .getResult () instanceof ResultSet ) {
80
+ data = Items .create ((ResultSet ) r .getResult ());
81
+ } else if (r .getResult () instanceof Model ) {
82
+ data = Items .create ((((Model ) r .getResult ()).getGraph ().find (null , null , null )));
83
+ } else if (r .getResult () instanceof Boolean ) {
84
+ data = Items .create ((Boolean ) r .getResult ());
85
+ } else if (r .getResult () instanceof List ) {
86
+ data = Items .create ((List <Map <String , String >>) r .getResult ());
125
87
}
126
88
view .getEngine ().exec (writer , view .getTemplate (), data );
127
89
// Yeah!
@@ -146,22 +108,15 @@ private Response performQuery(String id,
146
108
return buildNotAcceptable ();
147
109
}
148
110
149
- if (q .isSelectType ()) {
150
- entity = prepareEntity (type , qe .execSelect ());
151
- } else if (q .isConstructType ()) {
152
- entity = prepareEntity (type , qe .execConstruct (), q
153
- .getPrefixMapping ().getNsPrefixMap ());
154
- } else if (q .isAskType ()) {
155
- entity = prepareEntity (type , qe .execAsk ());
156
- } else if (q .isDescribeType ()) {
157
- entity = prepareEntity (type , qe .execDescribe (), q
111
+ Object entity = null ;
112
+ if (r .getResult () instanceof ResultSet ) {
113
+ entity = prepareEntity (type , (ResultSet ) r .getResult ());
114
+ } else if (r .getResult () instanceof Model ) {
115
+ entity = prepareEntity (type , (Model ) r .getResult (), r .getQuery ()
158
116
.getPrefixMapping ().getNsPrefixMap ());
159
- } else {
160
- return Response .serverError ()
161
- .entity ("Unsupported query type: " + q .getQueryType ())
162
- .build ();
117
+ } else if (r .getResult () instanceof Boolean ) {
118
+ entity = prepareEntity (type , (Boolean ) r .getResult ());
163
119
}
164
-
165
120
// If entity is null then format is not acceptable
166
121
// ie we don't have an implementation of that object/type map
167
122
if (entity == null ) {
@@ -719,10 +674,25 @@ public Response replaceSpec(
719
674
@ ApiParam (value = "SPARQL query that substitutes the API specification" , required = true )
720
675
String body ) {
721
676
log .trace ("Called PUT with id: {}" , id );
722
- if (!isValidId (id )) {
723
- return Response .status (400 ).build ();
677
+ try {
678
+ getApiManager ().replaceSpecification (id , body );
679
+
680
+ ResponseBuilder response ;
681
+ URI spec = requestUri .getBaseUriBuilder ().path (id ).path ("spec" ).build ();
682
+ log .info ("Replaced spec at: {}" , spec );
683
+ response = Response .ok (spec ).entity (
684
+ "Replaced: " + spec .toString () + "\n " );
685
+
686
+ addHeaders (response , id );
687
+
688
+ return response .build ();
689
+ } catch (SpecificationParsingException e ) {
690
+ return Response .status (HttpURLConnection .HTTP_BAD_REQUEST )
691
+ .header (Headers .Error , e .getMessage ()).build ();
692
+ } catch (IOException e ) {
693
+ return Response .status (HttpURLConnection .HTTP_INTERNAL_ERROR )
694
+ .header (Headers .Error , e .getMessage ()).build ();
724
695
}
725
- return new SpecificationResource ().doPUT (id , body );
726
696
}
727
697
728
698
/**
@@ -734,9 +704,6 @@ public Response replaceSpec(
734
704
@ GET
735
705
public Response redirectToSpec (
736
706
@ PathParam (value = "id" ) String id ) {
737
- if (!isValidId (id )) {
738
- return Response .status (400 ).build ();
739
- }
740
707
ResponseBuilder builder = Response .status (303 );
741
708
addHeaders (builder , id );
742
709
return builder .location (requestUri .getBaseUriBuilder ().path (id ).path ("spec" ).build ()).build ();
@@ -760,16 +727,11 @@ public Response getSpec(
760
727
@ PathParam (value = "id" ) String id ) {
761
728
log .trace ("Called GET spec with id: {}" , id );
762
729
try {
763
- if (!isValidId (id )) {
764
- return Response .status (400 ).build ();
765
- }
766
730
767
- Store store = getDataStore ( );
768
- if (! store . existsSpec ( id ) ) {
731
+ Specification spec = getApiManager (). getSpecification ( id );
732
+ if (spec == null ) {
769
733
return Response .status (Response .Status .NOT_FOUND ).build ();
770
734
}
771
-
772
- Specification spec = store .loadSpec (id );
773
735
ResponseBuilder response = Response .ok ();
774
736
response .header (Headers .Endpoint , spec .getEndpoint ());
775
737
addHeaders (response , id );
@@ -800,11 +762,59 @@ public Response deleteSpec(
800
762
@ ApiParam (value = "ID of the API specification" , required = true )
801
763
@ PathParam (value = "id" ) String id ) {
802
764
log .trace ("Called DELETE spec with id: {}" , id );
803
- if (!isValidId (id )) {
804
- return Response .status (400 ).build ();
765
+ try {
766
+ getApiManager ().deleteApi (id );
767
+ URI spec = requestUri .getBaseUriBuilder ().path (id ).path ("spec" ).build ();
768
+ ResponseBuilder response ;
769
+ response = Response .ok ().entity (
770
+ "Deleted: " + spec .toString ());
771
+ addHeaders (response , id );
772
+
773
+ return response .build ();
774
+ } catch (IOException e ) {
775
+ throw new WebApplicationException (Response
776
+ .status (HttpURLConnection .HTTP_INTERNAL_ERROR )
777
+ .entity (e .getMessage ()).build ());
805
778
}
806
- // XXX Not Implemented
807
- return Response .status (501 ).entity ("Not implemented yet\n " ).build ();
808
779
809
780
}
781
+
782
+ /**
783
+ * Gets a new clone of an API.
784
+ *
785
+ * @param id
786
+ * @return
787
+ */
788
+ @ GET
789
+ @ Path ("clone" )
790
+ @ Produces ("text/plain" )
791
+ @ ApiOperation (value = "Get a clone of an API" )
792
+ @ ApiResponses (value = {@ ApiResponse (code = 404 , message = "Specification not found" ),
793
+ @ ApiResponse (code = 200 , message = "API clones" ),
794
+ @ ApiResponse (code = 500 , message = "Internal error" )})
795
+ public Response getClone (
796
+ @ ApiParam (value = "ID of the API specification" , required = true )
797
+ @ PathParam (value = "id" ) String id ) {
798
+ log .trace ("Called GET clone with id: {}" , id );
799
+ try {
800
+
801
+ String newId = getApiManager ().cloneSpecification (id );
802
+ if (newId == null ) {
803
+ return Response .status (Response .Status .NOT_FOUND ).build ();
804
+ }
805
+ ResponseBuilder response ;
806
+ URI spec = requestUri .getBaseUriBuilder ().path (newId ).path ("spec" ).build ();
807
+ log .info ("Cloned spec at: {}" , spec );
808
+ response = Response .ok (spec ).entity (
809
+ "Cloned at: " + spec .toString () + "\n " );
810
+ addHeaders (response , newId );
811
+
812
+ return response .build ();
813
+ } catch (Exception e ) {
814
+ log .error ("An error occurred" , e );
815
+ throw new WebApplicationException (Response
816
+ .status (HttpURLConnection .HTTP_INTERNAL_ERROR )
817
+ .entity (e .getMessage ()).build ());
818
+ }
819
+ }
810
820
}
0 commit comments