Skip to content

Commit

Permalink
fix resource XMLs
Browse files Browse the repository at this point in the history
update foto for bitbucket finished
convert foto to JPG in read for midPoint
  • Loading branch information
gpalos committed Mar 31, 2016
1 parent 841933b commit 77caa01
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 48 deletions.
17 changes: 16 additions & 1 deletion samples/resources/scriptedrest/bitbucket/SearchScript.groovy
@@ -1,7 +1,12 @@
import groovyx.net.http.HttpResponseException

import javax.imageio.ImageIO
import java.awt.image.BufferedImage
import java.nio.file.Files

import static java.awt.RenderingHints.KEY_INTERPOLATION
import static java.awt.RenderingHints.VALUE_INTERPOLATION_BICUBIC

/*
* Copyright (c) 2010-2016 Evolveum
*
Expand Down Expand Up @@ -131,6 +136,7 @@ def parseUser(it, readImage, defaultAvatar) {
}

byte[] avatar = null; // send only not default profile pictures
byte[] avatarJpg = null;
if (readImage && avatarUrl != null) {
respImage = connection.get(path: avatarUrl,
headers: ['Content-Type': 'image/png'],
Expand All @@ -143,12 +149,21 @@ def parseUser(it, readImage, defaultAvatar) {
if (Arrays.equals(avatar, defaultAvatar)) {
avatar = null;
}
else { // convert it to JPG for midPoint
ByteArrayInputStream bis = new ByteArrayInputStream(avatar);
ByteArrayOutputStream bos = new ByteArrayOutputStream();

BufferedImage img = ImageIO.read(bis);
ImageIO.write(img, "jpg", bos);
//ImageIO.write(img, 'jpg', new File("D://gusto.jpg") )
avatarJpg = bos.toByteArray();
}

}

return [__UID__ : id,
__NAME__ : id,
avatarLink : avatarUrl,
avatar : avatar
avatar : avatarJpg
];
}
64 changes: 35 additions & 29 deletions samples/resources/scriptedrest/bitbucket/UpdateScript.groovy
Expand Up @@ -15,6 +15,7 @@
*/

import groovy.json.JsonSlurper
import org.apache.commons.codec.binary.Hex

import static java.awt.RenderingHints.*
import java.awt.image.BufferedImage
Expand Down Expand Up @@ -104,43 +105,48 @@ switch ( objectClass ) {
//connection.setProxy('localhost', 8888, 'http');


// body = '--------------------------d9644f1fd52e712e\n' +
// 'Content-Disposition: form-data; name="avatar"; filename="gusto.png"\n' +
// 'Content-Type: application/octet-stream\n\n' +
// ' PNG';
//
// ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );
// outputStream.write(body.getBytes());
// outputStream.write(avatar);
//
// byte bodyBytes = outputStream.toByteArray( );
String hexNewLine = "0D0A";
byte[] byteNewLine = Hex.decodeHex(hexNewLine.toCharArray());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );

// avatar as JPG - no conversion to PNG
// outputStream.write('--------------------------86c9c0934f0c28c5'.getBytes());
// outputStream.write(byteNewLine);
// outputStream.write('Content-Disposition: form-data; name="avatar"; filename="gusto.jpg'.getBytes());
// outputStream.write(byteNewLine);
// outputStream.write('Content-Type: image/jpeg'.getBytes());
// outputStream.write(byteNewLine);
// outputStream.write(byteNewLine);
//
// ByteArrayInputStream inputStream = new ByteArrayInputStream(body.getBytes()) //as byte[]
// outputStream.write(avatarOrig); //avatar as JPG
//
// resp = connection.post(path: "http://localhost:7990/rest/api/1.0/users/"+userName+"/avatar.png",
// headers: ['X-Atlassian-Token': 'no-check', 'Accept': '*/*', 'Content-Type': 'multipart/form-data; boundary=------------------------d9644f1fd52e712e'],
// contentType: 'application/octet-stream',
// body: body.getBytes());
// outputStream.write(byteNewLine);
// outputStream.write('--------------------------86c9c0934f0c28c5--'.getBytes());
// outputStream.write(byteNewLine);

ContentDisposition content = new ContentDisposition("form-data; name=\"avatar\"; filename=\"gusto.png\"");
ByteArrayInputStream bisResized = new ByteArrayInputStream(avatar) //as byte[]
Attachment avatarAtt = new Attachment("d9644f1fd52e712e", bisResized, content)
// avatar as converted PNG
outputStream.write('--------------------------86c9c0934f0c28c5'.getBytes());
outputStream.write(byteNewLine);
outputStream.write('Content-Disposition: form-data; name="avatar"; filename="avatar.png"'.getBytes());
outputStream.write(byteNewLine);
outputStream.write('Content-Type: application/octet-stream'.getBytes());
outputStream.write(byteNewLine);
outputStream.write(byteNewLine);

List<Attachment> atts = new LinkedList<Attachment>();
atts.add(avatarAtt);
outputStream.write(avatar); //avatar as PNG

def body = new MultipartBody(atts, MediaType.APPLICATION_OCTET_STREAM_TYPE, false)
outputStream.write(byteNewLine);
outputStream.write('--------------------------86c9c0934f0c28c5--'.getBytes());
outputStream.write(byteNewLine);

resp = connection.post(path: "http://localhost:7990/rest/api/1.0/users/"+userName+"/avatar.png",
headers: ['X-Atlassian-Token': 'no-check', 'Accept': '*/*', 'Content-Type': 'multipart/form-data'],
// contentType: 'application/octet-stream',
body: body);
byte[] bodyBytes = outputStream.toByteArray();

// requestContentType: 'multipart/form-data',
resp = connection.post(path: "http://localhost:7990/rest/api/1.0/users/"+userName+"/avatar.png",
headers: ['X-Atlassian-Token': 'no-check', 'Accept': '*/*', 'Content-Type': 'multipart/form-data; boundary=------------------------86c9c0934f0c28c5'],
contentType: 'application/octet-stream',
body: bodyBytes);

def slurper = new JsonSlurper();
def respJson = slurper.parseText(resp.getData().text) // need to manually convert
log.ok("response: {0}", respJson /* resp.getData()==null ? resp : resp.getData().text*/);
log.ok("response: {0}", resp.getData()==null ? resp : resp.getData());
}
}

Expand Down
18 changes: 12 additions & 6 deletions samples/resources/scriptedrest/bitbucket/resource.xml
Expand Up @@ -57,19 +57,19 @@
<minOccurs>0</minOccurs>
</limitations>
<matchingRule xmlns:mr="http://prism.evolveum.com/xml/ns/public/matching-rule-3">mr:stringIgnoreCase</matchingRule>
<inbound>
<strength>normal</strength>
<target>
<c:path>$user/name</c:path>
</target>
</inbound>
<!-- to detect existing users as AlreadyExistsException over CreateScript -->
<outbound>
<strength>normal</strength>
<source>
<c:path>$user/name</c:path>
</source>
</outbound>
<inbound>
<strength>normal</strength>
<target>
<c:path>$user/name</c:path>
</target>
</inbound>
</attribute>

<attribute>
Expand All @@ -84,6 +84,12 @@
<c:path>$user/jpegPhoto</c:path>
</source>
</outbound>
<inbound>
<strength>strong</strength>
<target>
<c:path>$user/jpegPhoto</c:path>
</target>
</inbound>
</attribute>

<protected>
Expand Down
12 changes: 6 additions & 6 deletions samples/resources/scriptedrest/confluence-wiki/resource.xml
Expand Up @@ -59,19 +59,19 @@
<minOccurs>0</minOccurs>
</limitations>
<matchingRule>mr:stringIgnoreCase</matchingRule>
<inbound>
<strength>normal</strength>
<target>
<c:path>$user/name</c:path>
</target>
</inbound>
<!-- to detect existing users as AlreadyExistsException over CreateScript -->
<outbound>
<strength>normal</strength>
<source>
<c:path>$user/name</c:path>
</source>
</outbound>
<inbound>
<strength>normal</strength>
<target>
<c:path>$user/name</c:path>
</target>
</inbound>
</attribute>

<attribute>
Expand Down
12 changes: 6 additions & 6 deletions samples/resources/scriptedrest/jira/resource.xml
Expand Up @@ -59,19 +59,19 @@
<minOccurs>0</minOccurs>
</limitations>
<matchingRule>mr:stringIgnoreCase</matchingRule>
<inbound>
<strength>normal</strength>
<target>
<c:path>$user/name</c:path>
</target>
</inbound>
<!-- to detect existing users as AlreadyExistsException over CreateScript -->
<outbound>
<strength>normal</strength>
<source>
<c:path>$user/name</c:path>
</source>
</outbound>
<inbound>
<strength>normal</strength>
<target>
<c:path>$user/name</c:path>
</target>
</inbound>
</attribute>
<attribute>
<c:ref>ri:emailAddress</c:ref>
Expand Down

0 comments on commit 77caa01

Please sign in to comment.