Skip to content

Commit

Permalink
#96 add full support of rest api
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Aug 21, 2017
1 parent 68b51d0 commit 6f01eb6
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
*/
package org.bsc.confluence;

import java.util.regex.Pattern;

import rx.functions.Action1;

import static java.lang.String.format;
/**
*
* @author bsorrentino
Expand Down Expand Up @@ -65,6 +68,10 @@ public String removeFrom( String endpoint ) {

}

public boolean match( String endpoint ) {
return Pattern.matches( format(".+(%s)[/]?", path), endpoint );
}

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@
*/
package org.bsc.confluence;

import static java.lang.String.format;
import static org.codehaus.swizzle.confluence.XMLRPCConfluenceServiceImpl.createInstanceDetectingVersion;

import java.io.File;
import java.io.InputStream;
import java.util.List;

import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;

import org.bsc.confluence.ConfluenceService.Credentials;
import org.bsc.confluence.rest.RESTConfluenceServiceImpl;
import org.bsc.confluence.rest.model.Page;
import org.bsc.ssl.SSLCertificateInfo;
import org.codehaus.swizzle.confluence.XMLRPCConfluenceServiceImpl;
import static org.codehaus.swizzle.confluence.XMLRPCConfluenceServiceImpl.createInstanceDetectingVersion;
import rx.functions.Action1;

import rx.functions.Action1;
/**
*
* @author bsorrentino
Expand All @@ -28,10 +32,13 @@ private static class MixedConfluenceService implements ConfluenceService {
final XMLRPCConfluenceServiceImpl xmlrpcService;
final RESTConfluenceServiceImpl restService;

public MixedConfluenceService(String endPoint, Credentials credentials, ConfluenceProxy proxyInfo, SSLCertificateInfo sslInfo) throws Exception {
public MixedConfluenceService(String endpoint, Credentials credentials, ConfluenceProxy proxyInfo, SSLCertificateInfo sslInfo) throws Exception {

this.xmlrpcService = createInstanceDetectingVersion(endpoint, credentials, proxyInfo, sslInfo);

this.xmlrpcService = createInstanceDetectingVersion(endPoint, credentials, proxyInfo, sslInfo);
this.restService = new RESTConfluenceServiceImpl(endPoint, credentials, sslInfo);
final String restEndpoint = ConfluenceService.Protocol.XMLRPC.removeFrom(endpoint);

this.restService = new RESTConfluenceServiceImpl(restEndpoint, credentials, sslInfo);
}

@Override
Expand Down Expand Up @@ -145,9 +152,36 @@ public void exportPage(String url, String spaceKey, String pageTitle, ExportForm

}

public static ConfluenceService createInstance(String endPoint, Credentials credentials, ConfluenceProxy proxyInfo, SSLCertificateInfo sslInfo) throws Exception {
/**
* return XMLRPC based Confluence services
*
* @param endpoint
* @param credentials
* @param proxyInfo
* @param sslInfo
* @return XMLRPC based Confluence services
* @throws Exception
*/
public static ConfluenceService createInstance( String endpoint,
Credentials credentials,
ConfluenceProxy proxyInfo,
SSLCertificateInfo sslInfo) throws Exception
{
if( ConfluenceService.Protocol.XMLRPC.match(endpoint)) {
return new MixedConfluenceService(endpoint, credentials, proxyInfo, sslInfo);
}
if( ConfluenceService.Protocol.REST.match(endpoint)) {
return new RESTConfluenceServiceImpl(endpoint, credentials /*, proxyInfo*/, sslInfo);
}

throw new IllegalArgumentException(
format("endpoint doesn't contain a valid API protocol\nIt must be '%s' or '%s'",
ConfluenceService.Protocol.XMLRPC.path(),
ConfluenceService.Protocol.REST.path())
);


return new MixedConfluenceService(endPoint, credentials, proxyInfo, sslInfo);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,22 @@
*/
package org.bsc.confluence.rest;

import static java.lang.String.format;

import java.io.IOException;
import java.io.Reader;
import static java.lang.String.format;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.stream.JsonParsingException;

import org.apache.commons.io.IOUtils;
import org.bsc.confluence.ConfluenceService;
import org.bsc.confluence.rest.model.Attachment;

import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
Expand All @@ -21,9 +29,6 @@
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.apache.commons.io.IOUtils;
import org.bsc.confluence.ConfluenceService;
import org.bsc.confluence.rest.model.Attachment;
import rx.Observable;

/**
Expand Down Expand Up @@ -82,7 +87,9 @@ protected Observable<Response> fromUrlGET( final HttpUrl url, final String descr
.get()
.build();

return fromRequest(req, description);
return fromRequest(req, description)
//.doOnNext( this::debugBody )
;
}

protected Observable<Response> fromUrlDELETE( final HttpUrl url, final String description ) {
Expand Down Expand Up @@ -126,16 +133,31 @@ protected Observable<Response> fromUrlPUT( final HttpUrl url, RequestBody inputB
return fromRequest(req, description);
}

protected void debugBody( Response res ) {

final ResponseBody body = res.body();

try {
System.out.printf( "BODY\n%s\n", new String(body.bytes()) );
} catch (IOException e) {
System.out.printf( "READ BODY EXCEPTION\n%s\n", e.getMessage() );
}

}
protected Observable<JsonObject> mapToArray( Response res) {

final ResponseBody body = res.body();

try (Reader r = body.charStream()) {

final JsonReader rdr = Json.createReader(r);

final JsonObject root = rdr.readObject();

if( !root.containsKey("results") ) {
return Observable.error( new Exception(root.toString()) );
}

// {"statusCode":404,"data":{"authorized":false,"valid":true,"errors":[],"successful":false,"notSuccessful":true},"message":"No space with key : TEST"}
final JsonArray results = root.getJsonArray("results");

Expand All @@ -150,10 +172,10 @@ protected Observable<JsonObject> mapToArray( Response res) {

return Observable.from( array );

} catch (IOException ex) {

return Observable.error(ex);
}
} catch (IOException | JsonParsingException e ) {
return Observable.error(e);
}

}

protected JsonObject mapToObject(Response res ) {
Expand Down Expand Up @@ -204,11 +226,19 @@ protected Observable<JsonObject> rxDescendantPages( final String id ) {
final HttpUrl url = urlBuilder()
.addPathSegment("content")
.addPathSegment(id)
.addPathSegments("descendant/page")
//.addPathSegments("descendant/page")
.addPathSegments("child/page")
.addQueryParameter("expand", EXPAND)
.build();
return fromUrlGET( url, "get descendant pafes" )
.flatMap( this::mapToArray );
return fromUrlGET( url, "get descendant pages" )
//.doOnNext( this::debugBody )
.flatMap( this::mapToArray )
.concatMap( (o) -> {
final String childId = o.getString("id");
return Observable.concat( Observable.just(o), rxDescendantPages(childId) );
})

;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@
*/
package org.bsc.confluence.rest;

import static java.lang.String.format;

import java.io.File;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.json.Json;
import javax.json.JsonObject;
import okhttp3.HttpUrl;
import javax.json.JsonObjectBuilder;

import org.bsc.confluence.ConfluenceService;
import org.bsc.confluence.ExportFormat;
import org.bsc.confluence.ConfluenceService.Model;

import rx.Observable;

import org.bsc.confluence.rest.model.Page;
import javax.json.JsonObjectBuilder;
import static java.lang.String.format;
import org.bsc.confluence.rest.model.Attachment;
import org.bsc.confluence.rest.model.Page;
import org.bsc.ssl.SSLCertificateInfo;
import org.codehaus.swizzle.confluence.ConfluenceExportDecorator;

import okhttp3.HttpUrl;
import rx.Observable;
import rx.functions.Action1;

/**
Expand Down Expand Up @@ -125,14 +125,16 @@ protected HttpUrl.Builder urlBuilder() {
int port = endpoint.getPort();
port = (port > -1 ) ? port : endpoint.getDefaultPort();

final String path = ConfluenceService.Protocol.XMLRPC.removeFrom(endpoint.getPath());

String path = endpoint.getPath();

path = (path.startsWith("/")) ? path.substring(1) : path;

return new HttpUrl.Builder()
.scheme(endpoint.getProtocol())
.host(endpoint.getHost())
.port(port)
.addPathSegments(path)
.addPathSegments(ConfluenceService.Protocol.REST.path())
//.addPathSegments(ConfluenceService.Protocol.REST.path())
;
}

Expand All @@ -148,7 +150,7 @@ public Model.PageSummary findPageByTitle(String parentPageId, String title) thro
.map( page -> new Page(page) )
.filter( page -> page.getTitle().equals( title ))
.toBlocking()
.first();
.firstOrDefault(null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
public class AbstractRestConfluence {
//private static final String URL = "http://192.168.99.100:8090/rest/api";
protected static String URL = "http://localhost:8090/";
protected static String URL = "http://localhost:8090/rest/api";

RESTConfluenceServiceImpl service;

Expand All @@ -55,7 +55,7 @@ public void getOrCreatePage() throws Exception {
final String title = "test-storage";

{
TestSubscriber<JsonObject> test = new TestSubscriber<>();
final TestSubscriber<JsonObject> test = new TestSubscriber<>();

service.rxfindPage(spaceKey,title).subscribe(test);

Expand Down Expand Up @@ -100,14 +100,15 @@ public void getOrCreatePage() throws Exception {
final Observable<JsonObject> error =
Observable.error(new Exception(format("parentPage [%s] doesn't exist!",parentPageTitle)));

TestSubscriber<JsonObject> test = new TestSubscriber<>();
final TestSubscriber<JsonObject> test = new TestSubscriber<>();

service.rxfindPage(spaceKey, parentPageTitle)
.switchIfEmpty( error )
.doOnNext( (t) -> System.out.printf( "Parent Id: [%s]\n", t.getString("id")) )
.flatMap( (parent) -> {
final String id = parent.getString("id");
final JsonObjectBuilder input = service.jsonForCreatingPage(spaceKey, Integer.valueOf(id), title0);
final JsonObjectBuilder input =
service.jsonForCreatingPage(spaceKey, Integer.valueOf(id), title0);

System.out.printf( "input\n%s\n", input.toString());

Expand Down Expand Up @@ -229,5 +230,23 @@ public void getOrCreatePageAndStoreStorage() throws Exception {

}

@Test //@Ignore
public void getDescendentsTest() throws Exception {

final String spaceKey = "TEST";
final String title = "Home";

final Model.Page page = service.getPage(spaceKey, title);

final java.util.List<Model.PageSummary> descendents = service.getDescendents( page.getId() );

Assert.assertThat( descendents, IsNull.notNullValue() );

for( Model.PageSummary p : descendents ) {
System.out.printf( "Descend Page: [%s]\n", p.getTitle());
}

}


}
10 changes: 6 additions & 4 deletions maven-confluence-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<properties>
<scmUrl>https://github.com/bsorrentino/maven-confluence-reporting-plugin.git</scmUrl>
<siteUrl>https://github.com/bsorrentino/maven-confluence-plugin</siteUrl>

<api>rpc/xmlrpc</api>
</properties>

<contributors>
Expand Down Expand Up @@ -84,7 +86,7 @@
<goal>delete</goal>
</goals>
<configuration>
<endPoint>http://support.softphone.it/confluence//rpc/xmlrpc</endPoint>
<endPoint>http://support.softphone.it/confluence/${api}</endPoint>
<spaceKey>~bsorrentino</spaceKey>
<parentPageTitle>Home</parentPageTitle>
<!-- TEST ISSUE 40 -->
Expand All @@ -107,7 +109,7 @@
</goals>
<configuration>

<endPoint>http://localhost:8090/rpc/xmlrpc</endPoint>
<endPoint>http://localhost:8090/${api}</endPoint>
<!--endPoint>https://localhost:8443/rpc/xmlrpc</endPoint-->
<spaceKey>TEST</spaceKey>
<parentPageTitle>Home</parentPageTitle>
Expand All @@ -134,7 +136,7 @@
<goal>delete</goal>
</goals>
<configuration>
<endPoint>http://localhost:8090/rpc/xmlrpc</endPoint>
<endPoint>http://localhost:8090/${api}</endPoint>
<spaceKey>TEST</spaceKey>
<parentPageTitle>TEST</parentPageTitle>
<!-- TEST ISSUE 40 -->
Expand All @@ -158,7 +160,7 @@
<!--
<endPoint>http://localhost:8090/rpc/xmlrpc</endPoint>
-->
<endPoint>http://support.softphone.it/confluence/rpc/xmlrpc</endPoint>
<endPoint>http://support.softphone.it/confluence/${api}</endPoint>
<spaceKey>~bsorrentino</spaceKey>
<parentPageTitle>Home</parentPageTitle>
<wikiFilesExt>.confluence</wikiFilesExt>
Expand Down

0 comments on commit 6f01eb6

Please sign in to comment.