Skip to content

Instantly share code, notes, and snippets.

@dsdstudio
Created April 26, 2012 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsdstudio/2497298 to your computer and use it in GitHub Desktop.
Save dsdstudio/2497298 to your computer and use it in GitHub Desktop.
extract manifest from url stream
package test;
import org.hamcrest.core.Is;
import org.junit.Test;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.jar.Attributes;
import static org.junit.Assert.assertThat;
/**
* Created with IntelliJ IDEA.
* User: bhkim
* Date: 12. 4. 25.
* Time: 오후 1:15
*/
public class JarManifestTest {
@Test
public void httpjarAttributes() {
Attributes attributes = this.getJarManifestAttribute("http://repo1.maven.org/maven2/org/eclipse/jetty/osgi/jetty-osgi-boot/8.1.3.v20120416/jetty-osgi-boot-8.1.3.v20120416.jar");
for (Map.Entry<Object, Object> entry : attributes.entrySet()) {
System.out.println("key :" + entry.getKey() + " | value :" + entry.getValue());
}
}
@Test
public void osgifiedvalidationtest() {
assertThat(isOsgifiedBundle(this.getJarManifestAttribute("http://repo1.maven.org/maven2/org/eclipse/jetty/osgi/jetty-osgi-boot/8.1.3.v20120416/jetty-osgi-boot-8.1.3.v20120416.jar")), Is.is(true));
assertThat(isOsgifiedBundle(this.getJarManifestAttribute("http://repo1.maven.org/maven2/javax/annotation/jsr250-api/1.0/jsr250-api-1.0.jar")), Is.is(false));
}
@Test
public void normalizetest() {
assertThat(normalize("http://repo1.maven.org/maven2/javax/annotation/jsr250-api/1.0/jsr250-api-1.0.jar"), Is.is("http://repo1.maven.org/maven2/javax/annotation/jsr250-api/1.0/jsr250-api-1.0.jar!/"));
}
public Attributes getJarManifestAttribute(String urlstr) {
URL url = null;
JarURLConnection uc = null;
try {
url = this.getJarfiedURL(urlstr);
uc = (JarURLConnection) url.openConnection();
return uc.getMainAttributes();
} catch (Exception e) {
// ignore
}
return null;
}
public boolean isOsgifiedBundle(Attributes attributes) {
return attributes != null && attributes.getValue("Bundle-SymbolicName") != null;
}
/**
* @param strurl
* @return
*/
public URL getJarfiedURL(String strurl) {
URL url = null;
try {
url = new URL("jar", "", normalize(strurl));
} catch (MalformedURLException e) {
}
return url;
}
/**
* Jar URL형태로 변환
*
* @param strurl
* @return
*/
public String normalize(String strurl) {
return strurl != null && !strurl.endsWith("!/") ? strurl + "!/" : strurl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment