[ckan-dev] Sever error: An internal server error occurred

Kim Haklae haklaekim at gmail.com
Wed Oct 10 01:03:40 UTC 2012


I used Model API for dataset register: curl
http://localhost/api/rest/dataset -d '{}' -H "Authorization: "

In case, the following code is to read data from rdb, and to register
dataset to a ckan.

========================
public class Uploader {
public static String APIKEY = "!api-key here!";
public static String URL = "http://localhost/api/rest/dataset";
 public static void main(String[] args) throws JSONException, IOException,
InterruptedException {
try {
Connection con = null;
String user = "root";
String password = "";
String url ="jdbc:mysql://localhost/html";
 con =
DriverManager.getConnection("jdbc:mysql://localhost/html?useUnicode=true&characterEncoding=euc_kr",
user, password);
 Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from resource");
 while (rs.next()) {
int idx = rs.getInt("id");
//String title = toUnicode(rs.getString("title"));
String title = rs.getString("title");
String webpage = rs.getString("webpage");
String resourceUrl = rs.getString("resourceUrl");
// more fields from db //
 int extras_index = 0;
 JSONObject js = new JSONObject();
JSONObject extras_item = null;
 JSONArray extras = new JSONArray();
js.put("extras", extras);
 JSONArray resource_arr = new JSONArray();
 if( !resourceUrl.isEmpty())
{
String [] resourcearr = resourceUrl.split(",");;
for (int j = 0; j < resourcearr.length; j++) {
 //System.out.println(resourcearr[j]);
String [] resource_name_url = resourcearr[j].split("=");
 //System.out.println(resource_name_url[0].toString() );
String resource_name = resource_name_url[0].trim();
String resource_url = resource_name_url[1].trim();
 JSONObject resources_item = new JSONObject();
resources_item.put("name", resource_name);
resources_item.put("url", resource_url);
resources_item.put("description", description);
resources_item.put("format", format);
resource_arr.put(resources_item);
 }
}
js.put("resources", resource_arr);
 JSONArray tag_arr = new JSONArray();
js.put("tags", tag_arr);
 if( !tag.isEmpty())
{
 String [] tagarr = tag.split(",");
for (int j = 0; j < tagarr.length; j++) {
tag_arr.put(j, tagarr[j].trim());
}
 }
 js.put("isopen", true);
js.put("title", title);
 String name = "hira-or-kr-"+ categoryCode + "-" + idx;
js.put("name", name);
js.put("notes", description);
js.put("author", agency);
js.put("license", "Creative Commons Attribution Share-Alike");
 js.put("url", webpage);
js.put("author", author);
js.put("maintainer", maintainer);
js.put("licence_id","cc-by-sa");
js.put("licence_url","http://www.opendefinition.org/licenses/cc-by-sa");
js.put("licence_title","Creative Commons Attribution Share-Alike");
 extras_item = new JSONObject();
extras_item.put("공개 상태", status);
extras.put(extras_index++, extras_item);
 extras_item = new JSONObject();
extras_item.put("분류", category);
extras.put(extras_index++,extras_item);
 extras_item = new JSONObject();
extras_item.put("공개일자", openDate);
extras.put(extras_index++,extras_item);
 extras_item = new JSONObject();
extras_item.put("제공기관", agency);
extras.put(extras_index++,extras_item);
 extras_item = new JSONObject();
extras_item.put("제공기관 URL", agencyUrl);
extras.put(extras_index++,extras_item);
 if(!department.isEmpty())
{
extras_item = new JSONObject();
extras_item.put("담당부서", department);
extras.put(extras_index++,extras_item);
}
if(!officer.isEmpty())
{
extras_item = new JSONObject();
extras_item.put("담당자", officer);
extras.put(extras_index++,extras_item);
}
if(!phone.isEmpty())
{
extras_item = new JSONObject();
extras_item.put("전화번호", phone);
extras.put(extras_index++,extras_item);
}
if(!email.isEmpty())
{
extras_item = new JSONObject();
extras_item.put("이메일", email);
extras.put(extras_index++,extras_item);
}
extras_item = new JSONObject();
extras_item.put("원본출처시스템", sourceSystem);
extras.put(extras_index++,extras_item);
 if(!note.isEmpty())
{
extras_item = new JSONObject();
extras_item.put("설명", note);
extras.put(extras_index++,extras_item);
}
 String string =  toGSON(js.toString());
System.out.println(string);
 string = Uploader.HTMLEncode(string);
 Process oProcess = new
ProcessBuilder("curl",Uploader.URL+"","-d",string,"-H","Authorization:"+Uploader.APIKEY).start();
InputStream is = oProcess.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }

br.close();
is.close();
oProcess.waitFor();
oProcess.destroy();
 }
con.close();
} catch (SQLException sqex) {
System.out.println("SQLException: " + sqex.getMessage());
System.out.println("SQLState: " + sqex.getSQLState());
}

}
 public static String toGSON(String jsonstring)
{
return  jsonstring.replaceAll("\\[\\{", "{").replaceAll("\\}\\]",
"}").replaceAll("\\}\\,\\{", ",");
 }

public static String HTMLDecode(String text)
{
try {
text = text.replaceAll("\\[u]", "-");
text = URLDecoder.decode(text,"UTF-8");

} catch (Exception e) {
try {
FileWriter longTerm= new FileWriter("utfdecode.txt",true);
longTerm.write(text+"\r\n");
longTerm.close();
} catch (Exception e2) {
// TODO: handle exception
}
;
// System.exit(1);

return text;
}
return text;
}

public static String HTMLEncode(String text)
{
try {
//text = text.replaceAll("/","QQQQQQQQ");
//text = text.replaceAll(":","AAAAAAAAAAA");
String s =URLEncoder.encode(text,"UTF-8");
//s = s.replaceAll("QQQQQQQQ","/");
//s = s.replaceAll("AAAAAAAAAAA",":");
return s;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return text;
}
public static String getStringFromFile(String filename, String encoding)
{
StringBuffer sb = new StringBuffer();
try {
 BufferedReader br = new BufferedReader(new InputStreamReader(new
FileInputStream(filename),encoding));
 String line = null;
 while( (line=br.readLine())!=null )
{
sb.append(line+"\n");
}
br.close();
} catch (Exception e) {
e.printStackTrace();

}
 return sb.toString();
}
 public static String StringReplace(String str){
      String match = "[^\uAC00-\uD7A3xfe0-9a-zA-Z\\s]";
      str =str.replaceAll(match, " ");
      return str;
}
 public static String toUnicode(String line)
{
String t = "";
try {
         for(int i = 0; i < line.length(); i++){
            char ch = line.charAt(i);
            t+=JavaUnicodeTest.unicodeToJava(ch);
        }
 } catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
return t;
}
}

On Wed, Oct 10, 2012 at 1:19 AM, Sean Hammond <sean.hammond at okfn.org> wrote:

> > The previous error message is from a local server. And I tested a same
> code
> > in thedatahub.kr (currently using ip address), and then get a same
> error.
> > But the error code is different:
> >
> > [Tue Oct 09 16:29:19 2012] [error] 2012-10-09 16:29:19,543 ERROR
> > [ckan.controllers.api] Format incorrect: Only lists of dicts can be
> placed
> > against subschema (u'resources',), not <type 'dict'> - {u'license':
>
> Hey, what API call are you making exactly?
>
> _______________________________________________
> ckan-dev mailing list
> ckan-dev at lists.okfn.org
> http://lists.okfn.org/mailman/listinfo/ckan-dev
>



-- 
Dr.Dr. Haklae Kim
Semantic Web and Open Data Hacker
http://thedatahub.kr
http://getthedata.kr
http://blogweb.co.kr
Tel: +82-(0)10-3201-0714
Who's Who in the World's 27th Edition - 2010
IBC 2000 Outstanding Scientists - 2010
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20121010/d2401cab/attachment-0001.html>


More information about the ckan-dev mailing list