Cookie
Cookieとは
ユーザー情報をパソコンに一時的に記録・参照する機能。
サーバーがWebブラウザを通じて訪問してきたユーザーのパソコンに一時的にデータを書き込んで保存する仕組み。
Cookieにはサーバー側が指定したデータを保存しておくことができる。
ユーザーIDを保存したとすると、次にそのサイトにアクセスした時に、自動的にユーザーの識別が行われ、そのユーザー用のサイトになる。
Cookieの保存先
クライアントのパソコンのCookieファイルに保存される。
サーバーがCookieをセットする
クライアントは、Cookieをセットしたサーバーへアクセスするたびに、そのCookie情報をサーバーへ送っている。
サーバーがクライアントへのレスポンスヘッダーにCookieを加える。
具体的な内容(Set-Cookie)
path
セットしたCookieが送信されるパスを指定
※domainとpathが一致した場合のみ、Cookieがサーバーへ送信される。
- secure
アクセス先が安全なサイトへの場合のみCookieを送信するようになる
IPアドレス
IPアドレスとは
ネットワーク上の住所。 プロバイダがIPアドレスを割り当ててくれる。
プロバイダとは、自分が使っている回線とインターネットをつなげてくれる仲介役。
IPアドレスの必要性
友達に手紙を送りたいと思っても、住所がないと手紙を届けることができないのと同じで、IPアドレスがないと通信ができない。
お互いのIPアドレスがわかっていることで、インターネットを介して通信が可能になっている。
グローバルIPアドレス
プライベートIPアドレス
ルーターとPC、PCとPCの間の通信を可能にさせるためのIPアドレス。
自宅や会社のルーターにグローバルIPアドレスが割り当てられ、そのルーターに接続しているPCにプライベートIPアドレスが割り当てられる。
動的IPアドレス
IPアドレスが変わる(可能性がある)IPアドレスの割り当て方。
IPアドレスは、プロバイダによって割り当てられるが、そのIPアドレスを使わなくなるとプロバイダに回収される。
そのため、IPアドレスが変わる可能性がある。し、変わらないこともある。
プロバイダがなるべく同じIPアドレスを割り当てるように頑張っているから、IPアドレスが変わらないこともある。
固定(静的)IPアドレス
LocalDateTimeをDBに保存できない
問題
LocalDateTime.now()
を引数としてDBに保存しようとすると下記のエラーが吐かれる。
ERROR [2017-05-24 01:46:42,641] io.dropwizard.jersey.errors.LoggingExceptionMapper: Error handling a request: a16f2a5e900f29b6 ! com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '\xAC\xED\x00\x05sr\x00\x0Djava.time.Ser\x95]\x84\xBA\x1B"H\xB2\x0C\x00\x00xpw\x0E\x05\x00\x00\x07\xE1\x05\x18\x0A.*$\xC6\xAC@x' for column 'date' at row 1 ! at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3968) ! at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3906) ! at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2524) ! at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2677) ! at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549) ! at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861) ! at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073) ! at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009) ! at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5098) ! at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994) ! at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
解決
LocalDateTime
とTimestamp
の変換ができるように、変換クラスを作成する。
LocalDateTimeConverter.java
package com.midcareer.midcommon.util; import java.sql.Timestamp; import java.time.LocalDateTime; import javax.persistence.AttributeConverter; import javax.persistence.Converter; @Converter(autoApply = true) public class LocalDateTimeConverter implements AttributeConverter<LocalDateTime, Timestamp> { @Override public Timestamp convertToDatabaseColumn(LocalDateTime localDateTime) { return (localDateTime == null ? null : Timestamp.valueOf(localDateTime)); } @Override public LocalDateTime convertToEntityAttribute(Timestamp timestamp) { return (timestamp == null ? null : timestamp.toLocalDateTime()); } }
Tableクラスの型がLocalDateTime
のプロパティに@Convert(converter = LocalDateTimeConverter.class)
を付け足す。
@Column(name="date") @Convert(converter = LocalDateTimeConverter.class) private LocalDateTime date;
無事DBに日時のデータを保存できるようになった。よかった。
とりあえずApache PDFBoxを使ってみる
まずApache PDFBoxをインストール(Gradle)
compile 'org.apache.pdfbox:pdfbox:2.0.5'
とりあえずコードを書いてみる
@Path("/job") public class Resource { @GET @UnitOfWork @Path("/pdf") @Produces(MediaType.TEXT_HTML) public PDDocument pdf() throws Exception { PDDocument document = new PDDocument(); PDPage blankPage = new PDPage(); document.addPage( blankPage ); // Save the newly created document document.save("BlankPage.pdf"); // finally make sure that the document is properly // closed. document.close(); return document; } }
実行したらエラー。
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=text/html.
調べてみたら、JSONサービスが足りてなかったっぽいので足す。
compile 'org.glassfish.jersey.media:jersey-media-moxy:2.22.2'
もう一回実行したらまたエラー。
ERROR [2017-05-01 11:49:39,487] io.dropwizard.jersey.errors.LoggingExceptionMapper: Error handling a request: d92592dce5a88ddf ! org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=text/html, type=class org.apache.pdfbox.pdmodel.PDDocument, genericType=class org.apache.pdfbox.pdmodel.PDDocument. ! at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:247)
@Produces(MediaType.TEXT_HTML)
から@Produces(MediaType.APPLICATION_JSON)
に変更してみた。
JSONで何かが帰ってきたから成功なのかこれは?
{“allSecurityToBeRemoved”:false,“documentInformation”:{},“resourceCache”:{},“version”:1.4}
BlankPage.pdfというファイルが新たに保存されていたから成功かな…
コンソールからJSONでPOSTする方法
curl -H 'Content-Type:application/json' -X POST -d "{"key":"value", "key":"value"}" http://
エラー
{"code":400,"message":"Unable to process JSON"}
解決方法
curl -H 'Content-Type:application/json' -X POST -d '{"key":"value", "key":"value"}' http://
オプション-d
を" "
から' '
に変更したらうまくいった。